如何在运行时更改连接字符串实体框架
我的appconfig文件中的代码:
<?xml version="1.0"?>
<configuration>
<connectionStrings>
<add name="RelationAtOfficeEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string="Data Source=.;Initial Catalog=RelationAtOffice;Integrated Security=True;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient"/>
</connectionStrings>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>
我想更改以下代码:
<?xml version="1.0"?>
<configuration>
<connectionStrings>
<add name="RelationAtOfficeEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string="Data Source=127.10.10.23,1356;Initial Catalog=RelationAtOffice;Integrated Security=True;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient"/>
</connectionStrings>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>
只是数据源发生了变化。
怎么做?
答案 0 :(得分:0)
实例化上下文时,它具有参数connectionString。你可以在那里改变它。