我想在我的 Web.config 中创建一个 connectionString,然后在所有模块声明的“提供程序连接字符串”属性中重新使用它。
例子: 以这种方式声明连接字符串:
<add name="MyConnectionString" connectionString="Data Source=.;Initial
Catalog=MyDB;User ID=username;Password=pwd;" />
然后在模块之间共享这个连接:
<add name="Module1Context" connectionString="metadata=res//*/Module1.csdl| ... |
...;provider=System.Data.SqlClient;provider connection string=MyConnectionString"
providerName="System.Data.EntityClient" />
这可能吗?
最佳答案
按照您上面描述的方式不可能直接实现。
这个解决方案几乎肯定比复制连接字符串、评论或取消评论条目要多得多。
请确保您使用的是 Configuration Transformation文件来管理它。
如果你真的必须这样做,那么你需要自己构建连接字符串,使用 EntityConnectionStringBuilder类,从您的 MyConnectionString
值中提取提供程序连接字符串。实例化时在上下文对象中设置 EntityConnection 属性;见http://msdn.microsoft.com/en-us/library/bb896325.aspx了解详情。
关于c# - Entity Framework 中的共享连接字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11144320/