EF向后兼容的数据库迁移 [英] EF backward compatible DB migrations

查看:203
本文介绍了EF向后兼容的数据库迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出如何使用EF code-第一和迁移实现以下部署方案。
这个想法是,我想和向后兼容的模式更改(例如:添加一列)和测试,一切仍在工作升级数据库。它是由绿/蓝部署的启发,但它并不完全是跟随模式。这样做的原因是以下这个过程:

I'm trying to figure out how to implement the following deployment scenario using EF code-first and migrations. The idea is that I would like to upgrade the DB with backward compatible schema changes (e.g.: add a column) and test that everything still works. It is inspired by green/blue deployment but it's not entirely following that pattern. The reasoning behind this is in following this process:


  1. 升级数据库(EF迁移)

  2. 测试网站

  3. Update网站code

  4. 如果出现错误,恢复到previous网站code

我肯定会面临的问题是,在步骤2(4)我一定会得到来自EF有关模型的错误被更改,尽管所有数据库的变化是与现有的code兼容...

The problem I will certainly face is that at step 2 (and 4) I will surely get an error from EF about the Model being changed, although all the DB changes are compatible with the existing code...

我知道,一个解决办法是将迁移向下数据库为previous版本(甚至做一个数据库备份),但它可能会发生一些迁移是非常复杂和向下的一部分可能被打破或者干脆不良codeD。

I know that a solution would be to migrate "Down" the database to the previous version (or even do a DB backup), but it may happen that some migrations are really complex and the "Down" part may be broken or simply poorly coded.

所以我的问题是:有没有办法避免EF检查型号或最终会意识到,变化是向后兼容

推荐答案

在dbinitializer设置为null将下降保持兼容检查,例如

Setting the dbinitializer to null will drop the compability check, e.g.

public class MyDBContext: DbContext 
{
    public MyDBContext() : base("myConnString")
    {            
        //Disable initializer
        Database.SetInitializer<MyDBContext>(null);
    }
    public DbSet<A> As { get; set; }
    public DbSet<B> Bs { get; set; }
}

也有人<一个href=\"http://stackoverflow.com/questions/10623260/how-can-i-disable-model-compatibility-checking-in-entity-framework-4-3\">here

这篇关于EF向后兼容的数据库迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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