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

时间:2021-08-11 15:40:59

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?

这可能吗?

2 个解决方案

#1


0  

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. Set the EntityConnection property in your context object when you instantiate it; see http://msdn.microsoft.com/en-us/library/bb896325.aspx for details.

如果你真的必须这样做,那么你需要自己构建连接字符串,使用EntityConnectionStringBuilder类,从MyConnectionString值中提取提供者连接字符串。在实例化它时,在上下文对象中设置EntityConnection属性;有关详细信息,请参阅http://msdn.microsoft.com/en-us/library/bb896325.aspx。

#2


0  

You can manage your connection string in runtime using ConfigurationManager.ConnectionStrings http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.connectionstrings.aspx

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

#1


0  

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. Set the EntityConnection property in your context object when you instantiate it; see http://msdn.microsoft.com/en-us/library/bb896325.aspx for details.

如果你真的必须这样做,那么你需要自己构建连接字符串,使用EntityConnectionStringBuilder类,从MyConnectionString值中提取提供者连接字符串。在实例化它时,在上下文对象中设置EntityConnection属性;有关详细信息,请参阅http://msdn.microsoft.com/en-us/library/bb896325.aspx。

#2


0  

You can manage your connection string in runtime using ConfigurationManager.ConnectionStrings http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.connectionstrings.aspx

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