混淆EF自动迁移和播种 - 每个程序启动播种 [英] Confusion over EF Auto Migrations and seeding - seeding every program start

查看:108
本文介绍了混淆EF自动迁移和播种 - 每个程序启动播种的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近更改了一个应用程序,使用以下的dev:

I recently changed an application from using the following for dev:

DropCreateDatabaseIfModelChanges<Context>



要使用:


To using:

 public class MyDbMigrationsConfiguration: DbMigrationsConfiguration<GrsEntities>
{
    public MyDbMigrationsConfiguration()
    {
        AutomaticMigrationsEnabled = true;
        AutomaticMigrationDataLossAllowed = true;
    }
}



在我的数据库上下文我有:


In my db context I have:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        // Tell Code First to ignore PluralizingTableName convention
        // If you keep this convention then the generated tables will have pluralized names.
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

        //set the initializer to migration
        Database.SetInitializer(new MigrateDatabaseToLatestVersion<GrsEntities, MigrationConfig>());
    }

我已经使用AddOrUpdate扩展名在DbMigrationsConfiguration中覆盖了Seed(context)只需使用添加之前的种子播放在drop db(DropCreateDatabaseIfModelChanges)。

I have overridden Seed(context) in DbMigrationsConfiguration using the AddOrUpdate extension where I was just using Add before with seeding on the drop db (DropCreateDatabaseIfModelChanges).

我的困惑是,迁移运行与应用程序的每个开始,无论是否有任何更改的DbContext。我每次启动应用程序(库运行一个服务)时,初始化程序就像种子一样运行。我的预期行为是检查迁移是否需要(幕后检查以查看模型是否匹配物理数据库),然后更新任何新的/删除的表/列,并且只有在某些更改后才运行种子。

My confusion is that the Migration runs with each start of the application regardless of there being any changes to the DbContext. Each and every time I start the application (library run through a service) the initializer runs as does the Seed. My expected behaviour is a check whether a migration is necessary (behind the scenes check to see if model matches physical db) then update any new/removed tables/columns and only run seed if something has changed.

在我的测试种子运行每次,这是可行的,但似乎效率低下,而不是我的预期。不幸的是,MSDN文档是非常有限的。

In my testing seed runs every time, which is workable but seemingly inefficient and was not what I expected. Unfortunately the MSDN documentation is quite limited.

我完全滥用MigrateDatabaseToLatestVersion?有没有办法获得我期望的行为(即只有种子,如果有一个模型改变),或者我应该只是改变我的种子方法,以期望运行每个应用程序启动?

Am I completely misusing MigrateDatabaseToLatestVersion? Is there any way to get the behaviour I expect (i.e. only seed if there is a model change) or should I just change my seed method to expect to be run every application launch?

推荐答案

仅当数据库更改时运行Seed方法对于在EF 4.1中运送的数据库初始化程序来说是非常有限的事实。这是有限的,因为有时你需要更新种子数据而不用更改数据库,但是为了实现这一点,你不得不人为地使数据库发生变化。

The fact that the Seed method ran only when the database changed was quite limiting for the database initializers that shipped in EF 4.1. It was limiting because sometimes you needed to update seed data without changing the database, but to make that happen you had to artificially make it seem like the database had changed.

迁移种子的使用变得有点不同,因为不能再假定数据库是空的 - 这就是Migrations的一点。因此,Migrations中的Seed方法必须假定数据库存在,并且可能已经有数据,但是可能需要更新数据以考虑对迁移数据库所做的更改。因此使用AddOrUpdate。

With Migrations the use of Seed became a bit different because it could no longer be assumed that the database was starting empty--that's kind of the point of Migrations after all. So a Seed method in Migrations has to assume that the database exists and may already have data in it, but that data might need to be updated to take into account the changes made to the database for the Migrations. Hence the use of AddOrUpdate.

所以现在我们有一种情况,必须写入种子来考虑现有数据,这意味着真的没有必要永久保存EF 4.1种子方法的限制,这样你就不得不让数据库看起来像是为了让Seed运行而改变。因此,种子现在在应用程序域中首次使用上下文时运行。这不应该改变种子被种植的方式,因为它需要处理数据已经存在的情况。

So now we have a situation where Seed must be written to take account of existing data, which means that there is really no need to perpetuate the limitations of the EF 4.1 Seed method such that you would have to make it seem like the database had changed just in order to get Seed to run. Hence Seed now runs every time the context is used for the first time in the app domain. This shouldn't change the way Seed is imlemented since it needs to handle the case where the data is already present anyway.

如果它导致perf问题,因为你有很多的种子数据,那么通常很容易添加对Seed方法的查询,查询数据库以确定在执行之前需要做多少工作。

If it causes perf issues because you have a lot of Seed data then it is usually quiet easy to add checks into the Seed method that query the database to determine how much work needs to be done before doing it.

这篇关于混淆EF自动迁移和播种 - 每个程序启动播种的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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