1

I want to change the ConnectionString in my app.config via code.

app.config:

<connectionStrings>
    <add name="MyEntities" 
         connectionString="metadata=res://*/MyEntities.csdl|res://*/MyEntities.ssdl|res://*/MyEntities.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=SomeServer\SomeInstance;initial catalog=SomeDatabase;persist security info=True;user id=USER_123;password=123;MultipleActiveResultSets=True;App=EntityFramework&quot;" 
         providerName="System.Data.EntityClient" />
</connectionStrings>

Code:

private void ApplyAppConfig(EnvironmentConfig envConfig)
{
    string connectionString = $"metadata=res://*/{envConfig.Database}.csdl|res://*/{envConfig.Database}.ssdl|res://*/M42Production.msl;provider=System.Data.SqlClient;provider" +
                              $@" connection string='data source={envConfig.DatabaseServer};initial catalog={envConfig.Database};persist security info=True;user id={envConfig.DatabaseUser};password={envConfig.DatabasePassword};" +
                              "MultipleActiveResultSets=True;App=EntityFramework'";

    var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
    var connectionStringsSection = (ConnectionStringsSection)config.GetSection("connectionStrings");
    connectionStringsSection.ConnectionStrings["MyEntities"].ConnectionString = connectionString;
    config.Save();
    ConfigurationManager.RefreshSection("connectionStrings");
}

Sadly I get an Exception:

System.ArgumentException: Keyword not supported: 'data source'.
at System.Data.Entity.Core.EntityClient.Internal.DbConnectionOptions.ParseInternal(IDictionary`2 parsetable, String connectionString, IList`1 validKeywords)
at System.Data.Entity.Core.EntityClient.Internal.DbConnectionOptions..ctor(String connectionString, IList`1 validKeywords)
at System.Data.Entity.Core.EntityClient.EntityConnection.ChangeConnectionString(String newConnectionString)
at System.Data.Entity.Core.EntityClient.EntityConnection..ctor(String connectionString)
at System.Data.Entity.Internal.LazyInternalConnection.InitializeFromConnectionStringSetting(ConnectionStringSettings appConfigConnection)
at System.Data.Entity.Internal.LazyInternalConnection.TryInitializeFromAppConfig(String name, AppConfig config)
at System.Data.Entity.Internal.LazyInternalConnection.Initialize()
at System.Data.Entity.Internal.LazyInternalConnection.CreateObjectContextFromConnectionModel()
at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
at System.Data.Entity.Internal.InternalContext.Initialize()
at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
at System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()
at System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider()

I've already tried this but with no success.

Is there anything else I am missing ?

Felix D.
  • 4,811
  • 8
  • 38
  • 72

0 Answers0