4

我正在创建一个 WPF 应用程序,具有经典架构:UI 层、业务逻辑层和基础架构层。我决定将配置拆分为两个文件:app.config 文件,包含常见的应用程序配置,以及 dll.config,包含在 DbContext 中用于域模型存储的连接字符串。第二个 .config 文件应该贴在业务逻辑 DLL 上,而第一个文件贴在相应的 UI 可执行文件上(会有更多的 UI 有它自己的配置)。

应用程序配置:

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

现在,当我启动应用程序时,DbContext 抛出一个异常,说它找不到具有指定名称的连接字符串。如果我将连接字符串从 dll.config 移动到 app.config - 一切正常。

也许我必须以某种方式显式加载配置?或者?..我做错了什么?

提前谢谢!

4

2 回答 2

3

根据提供的配置文件,Entity Framework 在启动项目中查找 root 的 app.config 并没有找到 EF 内容。特别是连接字符串。当您第一次使用 UF 时,它可能会在模型项目中创建此类条目。使用 EF5.0 的 IM 对实体部分的剪切和粘贴非常小心。

<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>
于 2012-11-30T11:13:10.070 回答
0

我希望我理解你的问题,你想将你的 app.configs 结构化为层次结构,这是有效的,在非常常见的 Web 应用程序中,让 .net 帮助你进行合并的最简单方法是指定应用程序的区域。在所有需要绑定的配置中配置,在内部配置中,您可以覆盖这些值,直到获得最后一个,例如:

根应用程序配置

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

内部应用配置

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

结果:

根应用程序配置

first : 1
second: 2
third: 3

内部应用配置

first: 10
second: 2
third: 3

编辑

对于您的 EF 上下文,在构造函数中设置连接字符串:

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

希望能帮助到你,

于 2012-11-28T11:50:21.170 回答