将自定义初始化程序的配置文件中的DatabaseInitializerForType设置为fire [英] Set DatabaseInitializerForType in config file for Custom Initializer to fire

查看:404
本文介绍了将自定义初始化程序的配置文件中的DatabaseInitializerForType设置为fire的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我要么得到错误,或者没有任何反应。我知道我有这样的自定义实体框架初始化器:

So I either get errors firing or nothing happens. I know that I have custom Entity Framework Initializer like this:

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; }
    }
}

然而,这一切都在一个类库中,我有一个控制台应用程序来测试它,它不会在app.config中启动。我最终希望创建一个基于环境的构建或者什么不是或者应用程序来启动或不启动。在浏览互联网之后,似乎很多不同的人在此配置设置中标记并添加不同的文字。我根据NuGet使用Entity Framework 6.1.3,但是配置和引用声明6.0.0,并且想知道这是否已经改变,以及如何使它从一个单独的项目中触发,而不必强硬启动。我有这个:

Yet this is all in a class library and when I have a console app to test this it will not fire in the app.config. I am ultimately looking to create builds or what not or apps to fire or not fire based on environment. After scouring the internet it seems many different people label and put different text in this config setting. I am using Entity Framework 6.1.3 according to NuGet, yet the config and references claim 6.0.0 and have wondered if this has changed and how to make it fire from a seperate project without having to hardcdoe the intializer to fire. I have this:

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

我已经尝试将值设置为Enabled,EasyEntity.EasyInitializer EasyEntity','true','comeonpleasefire'。但是,没有什么可以工作,因为我不知道该怎么做。我看到另一个博客把这个设置放在了配置部分的entityFramework节点里,但是也没有。我的控制台应用程序中的总配置设置可供参考:

I have tried setting 'value' to lots of things like 'Enabled', 'EasyEntity.EasyInitializer, EasyEntity', 'true', 'comeonpleasefire'. But nothing appears to work as I am ignorant of what to do. I saw another blog putting the setting inside the entityFramework node in the config section but that did not work either. My total config setting in my console app for reference:

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


推荐答案

实体框架不会从 appSettings 部分您的应用程序配置文件。相反,您需要使用 entityFramework.contexts.context.databaseInitializer 部分,这样做应该这样做:

Entity Framework doesn't pick up values from the appSettings section of your application configuration file. Instead you need to use entityFramework.contexts.context.databaseInitializer section, something like this should do it:

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

这篇关于将自定义初始化程序的配置文件中的DatabaseInitializerForType设置为fire的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆