如何在运行时更改连接字符串实体框架 [英] how to change connection string entity framework in runtime

查看:68
本文介绍了如何在运行时更改连接字符串实体框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在运行时更改连接字符串实体框架

我的appconfig文件中的代码:


how to change connection string entity framework in runtime

code in my appconfig file :


<?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>




我想更改以下代码:




I want to change the following code :

    <?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>




只是数据源已更改.




just data source changed.

How is it done?

推荐答案

如果使用非默认重载构造函数,则可以基于动态连接字符串创建objectcontext.将连接字符串的格式保留在appsettings中,并动态更改数据源并创建您的ObjectContext,直到时间模式保持不变,它将完全正常工作.


请参考文档. http://msdn.microsoft.com/en-us/library/system.data.objects.objectcontext.objectcontext.aspx [ ^ ]
You can create objectcontext based on dynamic connection string if you use the non-default overloaded constructor. Keep the format of connection string in appsettings and change the datasource dynamically and create your ObjectContext, till the time schema remains same it will work perfectly.


Please refer documentation at. http://msdn.microsoft.com/en-us/library/system.data.objects.objectcontext.objectcontext.aspx[^]


这篇关于如何在运行时更改连接字符串实体框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆