0

Ich versuche connection an externe Datei zu bewegen EntLib verwenden, aberEnterprise Library: Abschnitt connection Umleiten hinzufügen keine neuen Connection zu ConfigurationManager.ConnectionStrings

ConfigurationManager.ConnectionStrings gibt mir nur ASP.NET-Datenbank standardmäßig, die nicht einmal in Konfig.

Ich erwartete, dort zwei Verbindungszeichenfolge von shared.config zu sehen. Was kann das Problem sein, oder habe ich ein falsches Verständnis dieser Funktion?

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> 

Antwort

1

Eigentlich ConfigurationManager.ConnectionStrings weiß nichts über Enterprise Library Infrastruktur. Standardmäßig können Abschnitte nicht mit der ConfigurationSource der Enterprise Library umgeleitet werden.

Sie sollten configSource Attribut verwenden, das Feature von .NET Framework, nicht Enterprise Library ist.

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> 

Update: Wenn Sie Enterprise Library-Funktionen nutzen möchten, haben ConfigurationManager.ConnectionStrings nicht direkt verwenden. Verwenden Sie die Enterprise Library-Funktionen stattdessen zum Erstellen von Datenbankverbindungen wie:

Dann können Sie die Konfigurationsquellen der Enterprise Library verwenden.

Ich fand diese link with example nützlich.

+0

Als ich sah, Verbindungszeichenfolgen in ihrem Konfigurationstool umleiten dachte ich, es unterstützt default Section Redirect. Traurig, habe ich fast existiert :) Standard-Redirect-Mechanismus erlaubt nicht diese coolen Funktionen zum Erben und Zusammenführen von Konfigurationen – aershov

+0

Ich werde die Antwort aktualisieren – hal