实体框架迁移-启用自动迁移以及添加的迁移 [英] Entity Framework Migrations - Enable AutoMigrations along with added migration

查看:130
本文介绍了实体框架迁移-启用自动迁移以及添加的迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在项目中使用Entity Framework 4.3迁移.我想使用自动迁移,以便当我对域对象和上下文类进行修改时,我在运行项目时自动更新数据库.到目前为止,我已经完成了这项工作.

I'm utilizing Entity Framework 4.3 Migrations in my project. I would like to use Automatic migrations so that when I make modifications to my domain objects and my context class, my database automatically updates when I run the project. I have this working so far.

除了自动迁移之外,我还想使用一些已添加的迁移",并且我希望应用程序在运行该应用程序时自动跳至最新版本(基于我添加的迁移).

I would also like to use some Added Migrations in addition to the automatic migrations, and I would like the application to automatically jump to the latest version (based on my added migrations) when I run the application.

为此,我将其放置在global.asax文件中...

In order to do this I have placed this in the global.asax file...

Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyContext, Core.Migrations.Configuration>());

现在这可行,但是当我这样做时,它不再基于我的域对象自动更新数据库.

Now this works, but when I do this it no longer automatically updates the database based on my domain objects.

我希望能够完全删除数据库,然后运行该应用程序并运行所有自动迁移,然后运行我的显式迁移并将数据库升级到最新版本.

I would like to be able to completely delete the database and then run the application and have all the automatic migrations run and then have my explicit migrations run and bring the database up to the latest version.

我知道我在以前的项目中已经进行过此操作,但是我不确定在这种情况下我做错了什么.

I know I've had this working in a previous project, but I'm not sure what I'm doing wrong in this instance.

谢谢

推荐答案

您需要传递在构造函数中将AutomaticMigrationsEnabled设置为true的配置.这样的事情应该有所帮助:

You need to pass a configuration that has the AutomaticMigrationsEnabled set to true in the constructor. Something like this should help:


Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyContext, MyConfiguration>());

MyConfiguration类似于:

with MyConfiguration being something like:


public class MyConfiguration : Core.Migrations.Configuration
{
    public MyConfiguration { this.AutomaticMigrationsEnabled = true; }
}

免责声明:只是破解了此内容,因此可能需要进行一些细微调整才能使其编译

DISCLAIMER: Just hacked this in, so small tweaks might be required to get this to compile

只需使用EF 4.3.1进行检查,并且初始化程序的代码如下:

Just checked with EF 4.3.1 and the code is like this for the initializer:

Database.SetInitializer(new MigrateDatabaseToLatestVersion<DataContext, MyConfiguration>());

,此为配置类:

public class MyConfiguration : System.Data.Entity.Migrations.DbMigrationsConfiguration<DataContext>
{
    public MyConfiguration()
    {
        this.AutomaticMigrationsEnabled = true;
    }
}

这篇关于实体框架迁移-启用自动迁移以及添加的迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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