app.config中不支持关键字“数据源”

时间:2018-07-10 07:32:29

标签: c# entity-framework-6 app-config

我想通过代码更改const userId = 123; const userName = 'john' const url = `/foo/${userId}/${userName}`; console.log(url);中的ConnectionString

app.config:

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>

可悲的是,我得到了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"); }

Exception

我已经尝试过this,但没有成功。

还有什么我想念的吗?

0 个答案:

没有答案