How to specify Entity Framework Code The first connection string in an external Enterprise Library configuration file

I am creating a WPF application that has a classic architecture: user interface layer, business logic layer, and infrastructure layer. I decided to split the configuration into two files: the app.config file, which contains the general configuration of the application, and dll.config, which contains the connection string for use in DbContext to store the domain model. The second .config file must be attached to the business logic DLL, and the first file is attached to the corresponding user interface executable (there will be another user interface with its 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 launch the application, DbContext throws an exception saying that it cannot find the connection string with the specified name. If I translate the connection string from dll.config to app.config, everything works fine.

Maybe I should explicitly load the configuration somehow? Or? .. What am I doing wrong?

thanks in advance!

+4
source share
2 answers

Based on the supplied configuration files, the Entity Framework went to look in the app.config for the root in the startup project and did not find the EF content. Especially the Connection string. When you first used UF, he would have created such records, perhaps in a model project. IM using EF5.0 so carefully to cut and paste in the 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
source

I hope I understand your question, do you want to structure your app.configs as a hierarchy that is valid in web applications that are very common, the easiest way to let .net help you in merging, describing the regions of app.config in all of those configurations that should be limited, and in internal configurations you can override the values ​​to the last, for example:

ROOT APPCONFIG

 <configuration> <commonCollection> <add key="first" value="1" /> <add key="second" value="2" /> <add key="third" value="3" /> </commonCollection> </configuration> 

INNER APPCONFIG

 <configuration> <commonCollection> <remove key="first" /> <add key="first" value="10" /> </commonCollection> </configuration> 

Results:

ROOT APPCONFIG

 first : 1 second: 2 third: 3 

INNER APPCONFIG

 first: 10 second: 2 third: 3 

EDIT

for your EF context, to set the connection string in the constructor:

  public MyContextDB() :base("name=DefaultConnection") { //other initializers } 

hope this helps

0
source

All Articles