Why is databaser name (schema/catalog) hardcoded?

Posts   
 
    
Sire404
User
Posts: 17
Joined: 21-Mar-2005
# Posted on: 05-Dec-2022 14:24:18   

Using v5.6.1 .NET Standard 2.0 Adapter C#

How do I remove the database/schema/catalog name from being hardcoded into the model files? I cannot find any setting controlling this. Do I really need to edit template files?

We've been using CatalogNameOverwrites since 2009 but I cannot see any benefit of this compared to the .NET Core way with different connectionstrings in different appsettings and letting the EnvironmentName control things (appsettings.Development|Production.json)

It just seems risky and error-prone to hardcode database name - developers doesn't expect their connectionstring to be overwritten in code.

Perhaps this is because of backwards compatibility? In any case, I hope there's a way to opt out of catalog overwrites? Am I missing something?

Thanks!

Walaa avatar
Walaa
Support Team
Posts: 14946
Joined: 21-Aug-2005
# Posted on: 05-Dec-2022 22:37:46   

CatalogNames are hardcoded to support multiple catalogs in the same project. You can use CatalogNameOverwrites whether in config files or in code (.net core), and you can provide an empty string for the overwrite value, this will make sure whatever is there in the connectionString will be used.

Sire404
User
Posts: 17
Joined: 21-Mar-2005
# Posted on: 06-Dec-2022 09:14:41   

Thanks Walaa.

I guess my question is really, why is the convention that you HAVE to add a catalog overwrites when you have multiple databases? I'm guessing for historical reasons and backwards compatibility.

It would make more sense to use the .NET Core convention with connection strings in different appsettings.EnvironmentName.json.

Right now, it's easy for new developers to forget to add a catalog overwrite in all scenarios (unit tests, staging, production) - because you trust the catalog name in the connection string - with potentially serious consequences.

Have you considered a project setting that lets you disable hard coding the catalog name? This would solve my problems, unless it would break something else?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39588
Joined: 17-Aug-2003
# Posted on: 06-Dec-2022 09:49:16   

In e.g. SQL Server you can specify a catalog to connect to, but once you're connected you can access all catalogs your user has access to. We have the catalog name in the mappings because of multiple catalogs in the mappings and also because the wellformed names in sql server require 3 elements so to avoid problems at runtime in all situations.

Your suggestion of appsettings might look ok in your app, but not everyone creates .net core websites. Our generated code has to work for all apps it's used in, and this is the best way to do this.

Frans Bouma | Lead developer LLBLGen Pro
Sire404
User
Posts: 17
Joined: 21-Mar-2005
# Posted on: 06-Dec-2022 15:50:11   

I read this in the documentation:

You can also specify an empty string for the toName. In that case, the DQE will not specify a catalog name in the generated SQL elements, which will make the SQL target the catalog specified in the connection string.

So if I always set an empty overwrite, the catalog in my connectionstrings will work?

RuntimeConfiguration.ConfigureDQE<SQLServerDQEConfiguration>(c=>c.AddCatalogNameOverwrite("database", string.Empty));

Could this be a workaround?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39588
Joined: 17-Aug-2003
# Posted on: 06-Dec-2022 16:16:42   

That's right, that's what Walaa was referring to simple_smile

Frans Bouma | Lead developer LLBLGen Pro