EF向后兼容DB迁移 [英] EF backward compatible DB migrations

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

问题描述

我试图找出如何使用EF代码优先和迁移来实现以下部署方案。
这个想法是我想使用向后兼容的模式更改(例如:添加列)来升级数据库,并测试一切仍然可行。它受到绿色/蓝色部署的启发,但并不完全遵循这种模式。在此之后的原因是:


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

  2. 测试网站

  3. 更新网站代码

  4. 如果出现问题,请恢复到以前的网站代码

我一定会面对的问题是,在步骤2(和4),我一定会从EF收到有关正在更改的模型的错误,尽管所有数据库更改都与现有代码兼容...



我知道一个解决方案是将数据库Down到以前的版本(甚至做一个DB备份),但可能会发生一些迁移是非常复杂的,下部分可能会被破坏或编码简单。



所以我的问题是:有避免EF检查的方法模型或者最终会意识到这些变化是向后兼容的?

解决方案

将dbinitializer设置为null将会丢弃compabilit y检查,例如

  public class MyDBContext:DbContext 
{
public MyDBContext():base myConnString)
{
//禁用初始化程序
Database.SetInitializer< MyDBContext>(null);
}
public DbSet< A>作为{get;组; }
public DbSet< B> Bs {get;组; }
}

另外建议这里


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. Upgrade database (EF migration)
  2. Test website
  3. Update website code
  4. If something goes wrong, revert to the previous website 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...

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.

So my question is: is there a way to avoid EF checking the Model or eventually be aware that the changes are backward compatible?

解决方案

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; }
}

Also suggested here

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

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