EF - Code First 方法:未从 app.config 文件中选取连接字符串和初始化程序

EF - Code First approach: connection string and initializer are not being picked from app.config file

我有一个 class 库项目,我在其中创建了 Entity Framework 的实体和上下文。我还创建了一个初始化程序来为数据库中的默认数据提供种子。

以下是 class 库项目的 app.config 文件 -

<?xml version="1.0" encoding="utf-8"?>
<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=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" 
             requirePermission="false" />
  </configSections>
  <entityFramework>
    <contexts>
      <context type="WebExplore.Context.GeographyContext, WebExplore.Context">
        <databaseInitializer type="WebExplore.Context.WebExploreInitializer, WebExplore.Context" />
      </context>
    </contexts>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
  </entityFramework>
  <connectionStrings>
    <add name="WebExploreEntities"
        connectionString="Data Source=.\sqlexpress;Initial Catalog=WebExplore.Database;Integrated Security=true"
        providerName="System.Data.SqlClient"/>
  </connectionStrings>
</configuration>

但是,当我尝试使用 "Update-Database" 创建数据库时,它确实创建了一个具有默认名称且没有种子数据的数据库。

我相信,当我尝试通过 "Update-Database" 命令创建数据库时,不知何故 app.config 设置没有被选中。

任何人都可以通过将 app.config 设置纳入帐户来帮助我创建数据库和种子数据吗?

如有任何帮助,我们将不胜感激。

您正在尝试将 app.config 添加到您的 class 图书馆项目...您不能(请参阅 Can a class library have an App.config file?) .您需要将 app/web.config 文件添加到可以启动的项目(即实际应用程序)中。