在配置文件中设置 DatabaseInitializerForType 以触发自定义初始化程序

Set DatabaseInitializerForType in config file for Custom Initializer to fire

所以我要么触发错误,要么什么也没有发生。我知道我有这样的自定义 Entity Framework 初始化器:

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EasyEntity
{
    public class EasyInitializer : DropCreateDatabaseAlways<EasyContext>
    {
        protected override void Seed(EasyContext context)
        {
            List<Person> persons = new List<Person>
            {
                new Person { FirstName = "Waz", LastName = "Amattau"},
                new Person { FirstName = "Helena", LastName = "Handbasket"},
            };

            foreach (var person in persons)
                context.Person.Add(person);

            base.Seed(context);
        }
    }
}

当我很容易地设置它时,我可以调用它:

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EasyEntity
{
    public class EasyContext : DbContext
    {
        public EasyContext() : base("name=EasyEntity")
        {
            Database.SetInitializer<EasyContext>(new EasyInitializer());   
        }

        public DbSet<Person> Person { get; set; }
    }
}

然而,这一切都在 class 库中,当我有一个控制台应用程序来测试它时,它不会在 app.config 中触发。我最终希望根据环境创建构建或不创建应用程序来触发或不触发。在互联网上搜索后,似乎有很多不同的人在这个配置设置中贴上不同的标签并放置不同的文本。根据 NuGet,我正在使用 Entity Framework 6.1.3,但配置和引用声称 6.0.0 并且想知道这是否已经改变以及如何让它从一个单独的项目中触发而不必强制启动初始化程序。我有这个:

<appSettings>
    <add key="DatabaseInitializerForType EasyEntity.EasyInitializer, EasyEntity" value="true" />
  </appSettings>

我已经尝试将 'value' 设置为很多东西,例如 'Enabled'、'EasyEntity.EasyInitializer, EasyEntity'、'true'、'comeonpleasefire'。但是似乎没有任何效果,因为我不知道该怎么做。我看到另一个博客将设置放在配置部分的 entityFramework 节点内,但这也不起作用。我在控制台应用程序中的总配置设置以供参考:

<?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>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <appSettings>
    <add key="DatabaseInitializerForType EasyEntity.EasyInitializer, EasyEntity" value="true" />
  </appSettings>
  <connectionStrings>
    <add name="EasyEntity" providerName="System.Data.SqlClient" connectionString="Server=.;Database=Easy;Integrated Security=True;"/>
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="Data Source=.; Integrated Security=True; MultipleActiveResultSets=True" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>

Entity Framework 不会从应用程序配置文件的 appSettings 部分获取值。相反,您需要使用 entityFramework.contexts.context.databaseInitializer 部分,应该这样做:

  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="Data Source=.; Integrated Security=True; MultipleActiveResultSets=True" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
    <contexts>
        <context type="EasyEntity.EasyContext, EasyEntity">
            <databaseInitializer type="EasyEntity.EasyInitializer, EasyEntity" />
        </context>
    </contexts>
  </entityFramework>

EF 不会 运行 初始值设定项,直到您使用上下文或明确告诉 EF 创建数据库。

在您的控制台应用程序中试试这个

using (var context = new EasyContext ())
{
    // Uncomment this line to see the raw sql commands
    //context.Database.Log = Console.WriteLine;
    context.Database.CreateIfNotExists();
    context.Database.Initialize(true);
}

要回答原始问题,您可以appSettings 中设置初始化程序。 您可以像这样使用反射来设置初始化器:

<appSettings>
<add key="DatabaseInitializerForType EasyEntity.EasyContext, EasyEntity" value="System.Data.Entity.DropCreateDatabaseAlways`1[[EasyEntity.EasyContext, EasyEntity]], EntityFramework" />

您可能对 'disabled' 感到困惑,答案与将其设置为 'enabled' 或 'true' 大不相同。如果你想 运行 一个 'seed' 方法,你可以使用以下访问初始化器 class。

<appSettings>
<add key="DatabaseInitializerForType EasyEntity.EasyContext, EasyEntity"  value="EasyEntity.EasyInitializer, EasyEntity" />