实体框架5迁移:设置数据库的初始迁移和单一的种子 [英] Entity Framework 5 Migrations: Setting up an initial migration and single seed of the database

查看:104
本文介绍了实体框架5迁移:设置数据库的初始迁移和单一的种子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MVC4的应用程序,我最近升级到实体框架5,我试图超过移到我们的数据库使用迁移掉落和创建每次运行的开发风格。

I have an MVC4 app which I've recently upgraded to Entity Framework 5 and I am trying to move our database over to using migrations from the development style of dropping and creating each run.

下面是我在我的应用程序启动功能已经做到了。

Here's what I've done in my app start function.

protected void Application_Start()
{
    Database.SetInitializer(
        new MigrateDatabaseToLatestVersion< MyContext, Configuration >() );
    ...
}

我跑我的库项目启用的迁移命令,我认为这将创建一个初始迁移文件,但它创建的唯一文件配置

I ran the Enable-Migrations command on my repositories project and I thought that this would create an initial migration file however the only file it created was Configuration

当我删除它创建它通过code作为预期的第一和种子从配置文件数据库的数据库。在配置文件中我改变了所有的添加()功能 AddOrUpdate()

When I delete the database it creates it as expected via code first and seeds the database from the Configuration file. In the configuration file I changed all the Add() functions to AddOrUpdate()

然而,每一次文件的网站启动,并重复所有的种子数据一次又一次地运行在我的配置种子功能。

However it runs the seed function in my Configuration file each time the website starts and duplicates all the seed data again and again.

我想象它会创建一个初始迁移文件我阅读博客建议应该和我可以把种子数据在那里,但它没有

I imagined that it would create an initial migration file as the blog I read suggested that it would and I could put the seed data in there but it didn't

任何人能解释我应该如何在code被设置DB,使之仅仅种子一次?

Can anyone explain how I should be setting up DB in code so that it only seeds once?

<一个href=\"http://weblogs.asp.net/dotnetstories/archive/2012/11/20/looking-into-entity-framework-$c$c-first-migrations.aspx\">LINK:该迁移的博客文章我也跟着

虽然这是使用EF migrate.exe因为我已经转向使用回旋运行迁移。我仍然可以使用EF脚手架基于模型我迁移,但我写了一个小控制台应用程序编写迁移到SQL文件。然后我用回旋通过我的Rake构建脚本来执行迁移本身。还有涉及到多一点的过程,但它的稳定得多比使用EF当应用程序启动时执行上飞迁移。

While this is quite interesting for using the EF migrate.exe I've since switched to using roundhouse for running migrations. I still use EF to scaffold my migrations based on the models but I wrote a little console app to write the migrations out to SQL files. I then use roundhouse to perform the migrations themselves through my rake build scripts. There's a little more process involved but it's much more stable than using EF to perform the migrations on the fly when the application starts up.

推荐答案

这已被证明是一个受欢迎的职位,所以我已经在别人的反馈光更新它。要知道最主要的是,在配置类种子的方法是每次运行应用程序启动,这是不是有什么模板方法的注释暗示。见有人从微软这个答案<一个href=\"http://stackoverflow.com/questions/10822618/confusion-over-ef-auto-migrations-and-seeding-seeding-every-program-start\">post为什么这是 - 感谢杰森Learmouth用于查找

This has proved to be a popular post so I have updated it in light of feedback from others. The main thing to know is that the Seed method in the Configuration class is run EVERY time the application starts, which isn't what the comment in the template method implies. See the answer from someone at Microsoft to this post about why that is - thanks to Jason Learmouth for finding that.

如果你和我一样,只希望如果有任何未决的迁移,那么你需要做更多的工作来运行数据库更新。你可以发现,是否调用migrator.GetPendingMigrations()有挂起的迁移,但你必须做,在构造函数为待定迁移的列表被称为种子的方法之前被清除。在code来实现这一点,它出现在Migrations.Configuration类如下:

If you, like me, only want to run the database updates if there are any pending migrations then you need to do a bit more work. You can find that out if there are pending migrations by calling migrator.GetPendingMigrations(), but you have to do that in the ctor as the list of pending migrations is cleared before Seed method is called. The code to implement this, which goes in the Migrations.Configuration class is as follows:

internal sealed class Configuration : DbMigrationsConfiguration<YourDbContext>
{ 
    private readonly bool _pendingMigrations;

    public Configuration()
    {
        // If you want automatic migrations the uncomment the line below.
        //AutomaticMigrationsEnabled = true;
        var migrator = new DbMigrator(this);
        _pendingMigrations = migrator.GetPendingMigrations().Any();
    }

    protected override void Seed(MyDbContext context)
    {
        //Microsoft comment says "This method will be called after migrating to the latest version."
        //However my testing shows that it is called every time the software starts

        //Exit if there aren't any pending migrations
        if (!_pendingMigrations) return;

        //else run your code to seed the database, e.g.
        context.Foos.AddOrUpdate( new Foo { bar = true});
    }
}

我要指出,有人建议把种子code在实际的向上迁移code。这工作,但意味着你需要记得把种子code在每个新的迁移和pretty难记,所以我不会那样做。但是,如果每个移民的后裔的变化则可能是去了一个很好的方式。

I should point out that some people have suggested putting the seed code in the actual 'up' migration code. This works, but means you need to remember to put the seed code in each new migration and its pretty hard remember so I wouldn't do that. However if your seed changes with each migration then that might be the a good way to go.

这篇关于实体框架5迁移:设置数据库的初始迁移和单一的种子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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