Common Connection Strings in Entity Infrastructure

I want to create a single connectionString in my Web.config and then reuse it in the "provider connection string" attribute of all module declarations.

example: Declare a connection string this way:

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

and then exchange this connection between the modules:

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

Is it possible?

+4
source share
2 answers

This is directly possible, as described above.

The solution for this will almost certainly be FAR more work than just copying the connection strings, commenting or uncommenting the entries as you go.

Make sure you use Configuration Files to manage this.

If you really need to do this, 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 the context object when it is created; See http://msdn.microsoft.com/en-us/library/bb896325.aspx for more details.

0
source

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

0
source

All Articles