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

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

问题描述

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. You can call context.Database.CreateIfNotExists(); at application startup.

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

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

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

Include an EntityFramework Database Initializer via code:

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

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

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.

Include an EntityFramework Database Initializer via configuration element:

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

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

EDIT: Edited to respond to additional comments

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.

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

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

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

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