简体   繁体   English

实体框架中的共享连接字符串

[英]Shared Connection Strings in Entity Framework

I want to create a single connectionString in my Web.config and then re-use it in the "provider connection string" attribute of all Modules declarations. 我想在我的Web.config中创建一个connectionString,然后在所有模块声明的“provider connection string”属性中重用它。

example : Declare a connection string this way: 示例:以这种方式声明连接字符串:

<add name="MyConnectionString" connectionString="Data Source=.;Initial
Catalog=MyDB;User ID=username;Password=pwd;" />

and then share this connection between modules: 然后在模块之间共享此连接:

<add name="Module1Context" connectionString="metadata=res//*/Module1.csdl| ... | 
...;provider=System.Data.SqlClient;provider connection string=MyConnectionString" 
providerName="System.Data.EntityClient" />

Is this possible? 这可能吗?

您可以使用ConfigurationManager.ConnectionStrings在运行时管理连接字符串http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.connectionstrings.aspx

It's not directly possible the way you have it described above. 它不可能像上面描述的那样直接实现。

The solution for this is almost certainly going to be FAR more work than just copying your connection strings, commenting or uncommenting entries as you go. 对于这个问题的解决方案几乎可以肯定比仅仅复制连接字符串,评论或取消注释条目更多。

Be sure you are using Configuration Transformation files to manage this. 确保使用配置转换文件来管理它。

If you really must do this, then you will need to build the connection strings yourself, using the EntityConnectionStringBuilder class, pulling the provider connection string from your MyConnectionString value. 如果你真的必须这样做,那么你需要自己构建连接字符串,使用EntityConnectionStringBuilder类,从MyConnectionString值中提取提供者连接字符串。 Set the EntityConnection property in your context object when you instantiate it; 在实例化它时,在上下文对象中设置EntityConnection属性; see http://msdn.microsoft.com/en-us/library/bb896325.aspx for details. 有关详细信息,请参阅http://msdn.microsoft.com/en-us/library/bb896325.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM