实体框架4.3中递增种子数据的最佳方式 [英] Best way to incrementally seed data in Entity Framework 4.3

查看:79
本文介绍了实体框架4.3中递增种子数据的最佳方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在现有的数据库中使用了实体框架4.3,并且有几个我正在尝试的场景。

I have been using Entity Framework 4.3 on an existing database and I have a couple of scenarios that I am trying to cater for.

首先,如果我删除我的数据库我想要EF重新创建,如果从头开始 - 我已经成功地使用了一个CreateDatabaseIfNotExists数据库初始化器。

Firstly, if I delete my database I would like to EF to recreate if from scratch - I have successfully used a CreateDatabaseIfNotExists database initialiser for this.

其次,如果我更新我的模型,并且数据库已经存在我想要自动更新数据库 - 我已经成功地使用了Entity Framework 4.3迁移。

Secondly, if I update my model and the database already exists I would like the database to be updated automatically - I have successfully used Entity Framework 4.3 Migrations for this.

所以这是我的问题。说我在我的模型中添加一个新表,它需要一些参考数据,当数据库初始化运行时以及迁移运行时,最好的方式是确保数据创建。我的愿望是,当我从头开始创建数据库时,以及数据库由于迁移运行而被更新时,会创建数据。

So here's my question. Say I add a new table to my model which requires some reference data, what it the best way to ensure that this data gets created both when the database intialiser runs and also when the migration runs. My desire is that the data gets created when I'm creating the db from scratch and also when the database gets updated as the result of a migration running.

在某些EF迁移示例我已经看到人们在迁移的UP方法中使用SQL()函数来创建种子数据,但如果可能,我宁愿使用上下文来创建种子数据(如大多数数据库初始化器示例中所见),因为它似乎对我来说奇怪的是,当EF的整个想法抽象出来时,您将使用纯sql。我试图在UP方法中使用上下文,但是由于某种原因,当我尝试在调用下方添加种子数据以创建表时,它不认为在迁移中创建的表存在。

In some EF migrations examples I have seen people use the SQL() function in the UP method of the migration to create seed data but if possible I would rather use the context to create the seed data (as you see in most database initialiser examples) as it seems strange to me that you would use pure sql when the whole idea of EF is abstracting that away. I have tried to use the context in the UP method but for some reason it didn't think that a table that was created in the migration existed when I tried to add the seed data directly below the call to create the table.

非常感谢任何智慧。

推荐答案

如果要使用实体种子您应该在迁移配置中使用种子方法的数据。如果生成新项目启用迁移,您将获得此配置类:

If you want to use entities to seed data you should use Seed method in your migrations configuration. If you generate fresh project Enable-Migrations you will get this configuration class:

internal sealed class Configuration : DbMigrationsConfiguration<YourContext>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = false;
    }

    protected override void Seed(CFMigrationsWithNoMagic.BlogContext context)
    {
        //  This method will be called after migrating to the latest version.

        //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
        //  to avoid creating duplicate seed data. E.g.
        //
        //    context.People.AddOrUpdate(
        //      p => p.FullName,
        //      new Person { FullName = "Andrew Peters" },
        //      new Person { FullName = "Brice Lambson" },
        //      new Person { FullName = "Rowan Miller" }
        //    );
        //
    }
}

迁移方式如何种子数据不是非常有效,因为它应该用于一些非常基本的种子。每个新版本的更新将通过整套,并尝试更新现有数据或插入新数据。如果您不使用 AddOrUpdate 扩展方法,则必须手动确保数据仅在尚未存在的情况下才能种子数据库。

The way how migrations seed data are not very efficient because it is supposed to be used for some very basic seeding. Every update to new version will go through whole set and try to update existing data or insert new data. If you don't use AddOrUpdate extension method you must manually ensure that data are seeded to database only if they are not present yet.

如果你想要有效的播种方式,因为你必须种下很多数据,你会得到更好的结果与共同点:

If you want efficient way for seeding because you must seed o lot of data you will get better result with common:

public partial class SomeMigration : DbMigration
{
    public override void Up()
    {
        ...
        Sql("UPDATE ...");
        Sql("INSERT ...");
    }

    public override void Down()
    {
        ...
    }
}

这篇关于实体框架4.3中递增种子数据的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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