I am trying to move connectionStrings to external file using EntLib, but
ConfigurationManager.ConnectionStrings gives me only default ASP.NET database, which is not even in config.
data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true
I expected to see there two connection string from shared.config. What can be the problem, or do I have a wrong understanding this feature?
App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="enterpriseLibrary.ConfigurationSource" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ConfigurationSourceSection, Microsoft.Practices.EnterpriseLibrary.Common, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
</configSections>
<enterpriseLibrary.ConfigurationSource selectedSource="System Configuration Source">
<sources>
<add name="System Configuration Source" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.SystemConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
<add name="Shared" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
filePath="shared.config" />
</sources>
<redirectSections>
<add sourceName="Shared" name="connectionStrings" />
</redirectSections>
</enterpriseLibrary.ConfigurationSource>
</configuration>
shared.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="connStr1" connectionString="Data Source=local;Initial Catalog=DB1;Integrated Security=True"
providerName="System.Data.SqlClient" />
<add name="connStr2" connectionString="Data Source=local;Initial Catalog=DB2;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
Actually,
ConfigurationManager.ConnectionStringsdoes not know anything about Enterprise Library infrastructure. By default, sections cannot be redirected with Enterprise Library's ConfigurationSource.You should use
configSourceattribute, which is feature of .NET Framework, not Enterprise Library.app.config:
sharedConnectionStrings.config:
Update: If you want to use Enterprise Library features, don't use
ConfigurationManager.ConnectionStringsdirectly. Instead, use Enterprise Library features to create database connections like:Then you can use Enterprise Library's configuration sources.
I found this link with example useful.