企业库:重定向connectionStrings部分不会向ConfigurationManager.ConnectionStrings添加新的ConnectionStrings

时间:2016-04-05 17:29:26

标签: c# .net app-config enterprise-library enterprise-library-5

我正在尝试使用EntLib将connectionStrings移动到外部文件,但

ConfigurationManager.ConnectionStrings只给我默认的ASP.NET数据库,甚至在配置中也没有。

data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true

我希望看到shared.config中有两个连接字符串。 可能是什么问题,或者我对此功能的理解有误?

的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>

1 个答案:

答案 0 :(得分:1)

实际上,ConfigurationManager.ConnectionStrings对企业库基础架构一无所知。 默认情况下,无法使用Enterprise Library的ConfigurationSource重定向节。

您应该使用configSource属性,这是.NET Framework的功能,而不是Enterprise Library。

的app.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <connectionStrings configSource="sharedConnectionStrings.config" />
</configuration>

sharedConnectionStrings.config:

<?xml version="1.0" encoding="utf-8" ?>
<connectionsStrings>
    <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" />
</connectionsStrings>

更新:如果您要使用Enterprise Library功能,请不要直接使用ConfigurationManager.ConnectionStrings。而是使用Enterprise Library功能创建数据库连接,如:

 Database db = DatabaseFactory.CreateDatabase(“Tom’s Connection”);

然后您可以使用Enterprise Library的配置源。

我发现这个link with example很有用。