How to specify Entity Framework Code First connection string in external Enterprise Library config file

code-first connection-string database enterprise-library entity-framework

Question

I am creating a WPF application, having classical architecture: UI layer, business logic layer and infrastructure layer. I decided to split configuration in two files: app.config file, containing common app configuration, and dll.config, containing connection string to use in DbContext for domain model storage. The second .config file should be sticked to business logic DLL, while first file sticked to corresponding UI executable (there will be one more UI with it's own configuration).

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, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
  </configSections>

  <enterpriseLibrary.ConfigurationSource selectedSource="Winter DAL Configuration">
    <sources>
      <add name="Winter DAL Configuration" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        filePath="dll.config" />
    </sources>
  </enterpriseLibrary.ConfigurationSource> 
</configuration>

dll.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
  </configSections>
  <dataConfiguration defaultDatabase="WinterContext" />
  <connectionStrings>
    <add name="WinterContext" connectionString="Data Source=Winter.sdf"
      providerName="System.Data.SqlServerCe.4.0" />
  </connectionStrings>
</configuration>

Now, when I'm starting the app, DbContext throws an exception saying it cannot find connection string with specified name. If I move connection string from dll.config to app.config - all working fine.

Maybe I must explicitly load configuration somehow? Or?.. What do I do wrong?

Thx in advance!

1
4
12/2/2012 9:08:45 PM

Popular Answer

Based on the config files supplied, Entity Framework went looking in app.config of root in start project and didnt find the EF content. Especially the Connection string. When you first used UF it would have created such entries, perhaps in the model project. IM using EF5.0 so careful with cut and paste on Entity Section .

<configuration>
 <configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<startup>
  <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<entityFramework>
 <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
   <parameters>
     <parameter value="v11.0" />
   </parameters>
 </defaultConnectionFactory>
</entityFramework>
<connectionStrings>

<add name="CONTEXT_NAME_HERE"
     providerName="System.Data.SqlClient"
     connectionString="Data Source=YOURDB_SERVER;Initial Catalog=YOUR_DBNAME;Integrated Security=True;MultipleActiveResultSets=True;App=EntityFramework" />

</connectionStrings>
</configuration>
3
11/30/2012 11:13:10 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow