实体框架5迁移:设置数据库的初始迁移和单个种子

Nei*_*eil 28 asp.net-mvc entity-framework ef-code-first asp.net-mvc-4 entity-framework-5

我有一个MVC4应用程序,我最近升级到Entity Framework 5,我试图将我们的数据库移动到使用从删除和创建每个运行的开发风格的迁移.

这是我在app启动功能中所做的.

protected void Application_Start()
{
    Database.SetInitializer(
        new MigrateDatabaseToLatestVersion< MyContext, Configuration >() );
    ...
}
Run Code Online (Sandbox Code Playgroud)

我Enable-Migrations在我的存储库项目上运行了命令,我认为这将创建一个初始迁移文件,但它创建的唯一文件是Configuration

当我删除数据库时,它首先通过代码按预期创建它,并从配置文件中播种数据库.在配置文件中,我将所有Add()功能都更改为AddOrUpdate()

但是,Configuration每次网站启动时,它都会在我的文件中运行种子功能,并一次又一次地复制所有种子数据.

我想象它会创建一个initial migration文件,因为我读过的博客建议我会将种子数据放在那里,但它没有

任何人都可以解释我应该如何在代码中设置数据库,以便它只播种一次?


链接:我关注的迁移博客文章


虽然这对于使用EF migrate.exe非常有趣,但我已经转而使用roundhouse来运行迁移.我仍然使用EF来基于模型来构建我的迁移,但是我编写了一个小的控制台应用程序来将迁移写入SQL文件.然后我使用roundhouse通过我的rake构建脚本自己执行迁移.还有一些涉及的过程,但它比使用EF在应用程序启动时动态执行迁移要稳定得多.

Jon*_*ith 39

这已被证明是一个受欢迎的帖子,所以我根据其他人的反馈更新了它.要知道的主要是每次应用程序启动时都会运行Configuration类中的Seed方法,这不是模板方法中的注释所暗示的.见有人从微软这个问题的答案后,为什么那就是-感谢杰森Learmouth的发现.

如果您像我一样,只想在有任何挂起的迁移时运行数据库更新,那么您需要做更多的工作.您可以通过调用migrator.GetPendingMigrations()来查找是否存在挂起的迁移,但是您必须在ctor中执行此操作,因为在调用Seed方法之前会清除挂起的迁移列表.实现此目的的代码,在Migrations.Configuration类中,如下所示:

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});
    }
}
Run Code Online (Sandbox Code Playgroud)

我应该指出,有些人建议将种子代码放在实际的"向上"迁移代码中.这有效,但意味着您需要记住将种子代码放在每个新的迁移中并且非常难以记住,所以我不会这样做.但是,如果您的种子随着每次迁移而变化,那么这可能是一个很好的方法.


old*_*ard 6

您可以手动添加迁移并使用您想要的任何种子代码填充它吗?在包管理器控制台中运行:

Add-Migration [Name]
Run Code Online (Sandbox Code Playgroud)

然后,您可以编辑在迁移文件夹中为您创建的文件.

在我的项目中,我实际上像Richard一样在上下文配置的Seed方法中播种.我真的没有偏好.但迁移应该更高效,因为应用程序不需要在应用程序启动时检查数据库中是否存在行.只需要检查迁移是否已经运行,这应该更快.

internal sealed class Configuration : DbMigrationsConfiguration<MyContext>
{
    public Configuration()
    {
        // If you want automatic migrations as well uncomment below.
        // You can use both manual and automatic at the same time, but I don't recommend it.
        //AutomaticMigrationsEnabled = true;
        //AutomaticMigrationDataLossAllowed = true;
    }

    protected override void Seed(MyContext 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.

        context.FontFamilies.AddOrUpdate(
            f => f.Id,
            new FontFamily { Id = 1, PcName = "Arial" },
            new FontFamily { Id = 2, PcName = "Times New Roman" },
        });
Run Code Online (Sandbox Code Playgroud)

我在Global.asax中使用它:

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        // Any migrations that haven't been applied before will
        // automatically be applied on Application Pool restart

        Database.SetInitializer<MyContext>(
            new MigrateDatabaseToLatestVersion<MyContext,
            MyApp.Migrations.Configuration>()
        );
    }
}
Run Code Online (Sandbox Code Playgroud)