没有使用Entity Framework迁移进行初始创建 [英] No initial create with Entity Framework migrations

查看:80
本文介绍了没有使用Entity Framework迁移进行初始创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使Entity Framework迁移正常进行.我已经启用了代码优先迁移功能,它创建了一个迁移文件夹,配置文件和mig历史记录表,但是没有初始创建.我错过了一步吗?这是由EF(4.3.1)创建的新数据库.

I'm trying to get Entity framework migrations working. I've enabled code first migrations, its created a migrations folder, config file and the mig history table, but no initial create. Am i missing a step? This is a new db created by EF (4.3.1).

推荐答案

此行为默认情况下不存在,但是您可以通过许多不同的形式轻松获得它.

This behavior is not in place by default, but it is available to you easily in many different forms.

  1. 您可以在应用程序启动时调用context.Database.CreateIfNotExists();.

您可以使用内置的DatabaseInitializer之一. CreateDatabaseIfNotExists初始化程序内置于EntityFramework中,只需将其添加到您的项目中即可.

You can use one of the built-in DatabaseInitializers. The CreateDatabaseIfNotExists initializer is built into EntityFramework and just needs to be added to your project.

您可以创建自己的自定义数据库初始化程序,该初始化程序在其内部包含选项#1.示例:代码优先迁移和初始化

You could create your own custom database initializer which includes option #1 inside of itself. Example: Code First Migrations and initialization

您可以通过代码或配置文件将DatabaseInitializers包含在项目中.

You can include DatabaseInitializers in your project either by code or via a config file.

通过代码包含EntityFramework数据库初始化程序:

在应用程序启动时,您可以像这样设置DatabaseInitializer:

In your application startup you can setup the DatabaseInitializer like so:

System.Data.Entity.Database.SetInitializer<DairyMmmContext>(new System.Data.Entity.CreateDatabaseIfNotExists<DairyMmmContext>());

注意:在实体框架的整个生命周期中,此代码已多次更改!此示例适用于EF 4.3,这是可通过nuget获得的当前生产版本.

NOTE: this code has changed multiple times throughout the life of entityframework! This example is for EF 4.3 which is the current production release available via nuget.

通过配置元素包括EntityFramework数据库初始化程序:

<configuration>
  <entityFramework>
    <contexts>
      <context type="MyNamespace.MyEFDataContext, AssemblyName">
        <databaseInitializer
          type="System.Data.Entity.CreateDatabaseIfNotExists`2[[MyNamespace.MyEFDataContext, AssemblyName],
               [MyNamespace.Migrations.Configuration,  AssemblyName]], EntityFramework" />
      </context>
    </contexts>
  </entityFramework>
</configuration>

您会注意到,使用此配置可​​能有点不舒服".您需要将上面的AssemblyName替换为保留实体框架内容的程序集的名称,将MyNamespace.MyEFDataContext替换为您实体框架数据上下文的完全限定的名称,并将MyNamespace.Migrations.Configuration替换为您的配置类的完全限定的名称(默认情况下,位于项目内的迁移"文件夹中).

You'll notice this can be a little "ungraceful" with this configuration. You need to replace AssemblyName above with the name of the assembly you keep your entityframework stuff in, replace MyNamespace.MyEFDataContext with the fully qualified name of your entityframework data context, and replace MyNamespace.Migrations.Configuration with the fully qualified name to your configuration class (by default in the Migration folder inside your project).

编辑后可以回复其他评论

迁移是从一个模式定义到另一个模式定义的更改.创建空数据库不是迁移(但之后的所有操作都是迁移).您的项目中将没有迁移源文件,仅用于创建一个空的db,这是由初始化程序通过代码完成的.

A migration is a change from one schema definition to another schema definition. Creating the empty database is not a migration (but everything after that is). There will be no migration source file in your project for just creating an empty db, that is done in code by the initializer.

如果您已经在使用DropCreateDatabaseAlways初始化程序,则应该这样做.但是,我注意到您正在代码中设置初始值设定项,这意味着存在计时问题的机会(在您的上下文已经超出调用任何初始值设定项的时间之后设置初始值设定项).

If you are already using the DropCreateDatabaseAlways initializer it should be doing that. However, I noticed you are setting the initializer in code which means there is the opportunity for a timing problem (setting the initializer after your context is already past the point of calling any initializers).

您可以使用context.Database.Initialize(true);强制实体框架在代码中的任何点运行初始化器(参数为true/false可以强制初始化,而不管当前状态如何).这将删除并每次重新创建数据库.

You can force entityframework to run your initializer at any point in code with context.Database.Initialize(true); (The parameter is a true/false to force the initialization regardless of the current state). That would drop and recreate your database every time.

但是您也可以确保在应用程序的生命周期中尽早设置了初始化程序(在创建上下文的单个实例之前).

But you can also just make sure your initializer is setup as early as possible in your application's life cycle (before you have created a single instance of your context).

这篇关于没有使用Entity Framework迁移进行初始创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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