SyndRain
SyndRain

Reputation: 3785

How to prevent custom ConfigurationBuilder affecting the XSD designer?

I have a custom configuration builder that changes database connection strings based on the current environment configuration using the concept of this answer as shown in the code below. To my surprise, this customization that I thought only affects runtime also affects the Dataset designer. This is not desired.

I want to just use the same fixed connection strings when adding and modifying the queries and schemas in the XSD designer. Not only that, this also causes the problem that if I accidently add a table adapter or updates even a tiny bit of a table adapter, it changes the database prefix of the generated code to the based on the current connection string configuration (like the problem described in How to stop XSD designer from prefixing database names to tables in XSD codebehind). E.g. undesired changed value of DbObjectName and DataSourceName:

<DbSource ConnectionRef="DefaultConnection (Web.config)" DbObjectName="TESTDB.dbo.MyTable" DbObjectType="Table" GenerateShortCommands="true" GenerateMethods="Both" ... >
    ...
    <Parameters>
        <Parameter AllowDbNull="true" AutogeneratedName="Weight" ColumnName="Weight" DataSourceName="TESTDB.dbo.MyTable" DataTypeServer="int" DbType="Int32" Direction="Input" ParameterName="@Weight" Precision="0" ProviderType="Int" Scale="0" Size="4" SourceColumn="Weight" SourceColumnNullMapping="false" SourceVersion="Current" />
...

The pain is that since I often develop and run debug sessions under the "development"/"beta" environment, right now whenever I need to modify or add a dataset, I need to switch back to production, rebuild, and then re-open the dataset designer to prevent this from happening... Visual Studio (2022) often don't reload the configuration settings of the designer without all these steps. (Sometimes even worst. I'll need to restart the IDE...) Also I don’t want to worry about forgetting to switch the configuration profile before using the designer every time.

My question is: Is it possible to make sure that the XSD designer always use a fixed set of connection string when I add or modify table adapters, without being affected by the custom configuration? Or are there other methods to set the connection string during runtime?

This is the code of the custom configuration builder:

public class MyConnectionStringConfigurationBuilder : ConfigurationBuilder
{
// symbol defined in the `.csproj` file for each configuration profile
#if TESTING_ENV 
    private const string DefaultEnvironmentName = "Testing";
#else
    private const string DefaultEnvironmentName = "Production";
#endif
    public override ConfigurationSection ProcessConfigurationSection(ConfigurationSection configSection)
    {
        switch (configSection)
        {
            case ConnectionStringsSection connectionStringsSection:
                if(DefaultEnvironmentName == "Testing") {
                    connectionStringsSection.ConnectionStrings["DefaultConnection"].ConnectionString = "testing connection string to test DB";
                }
                return connectionStringsSection;
            default:
                return base.ProcessConfigurationSection(configSection);
        }
    }
}

Upvotes: 1

Views: 75

Answers (0)

Related Questions