检查应用的迁移是否与DbContext相匹配? [英] Check if applied migrations match the DbContext?

查看:60
本文介绍了检查应用的迁移是否与DbContext相匹配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个单元测试,以确保没有相应的迁移,任何开发人员都不会提交模型更改。

I want to create a unit test to ensure no developer will commit model changes without the corresponding migration.

如何测试数据库与DbContext匹配?

How do I test that the database matches the DbContext?

推荐答案

您可以利用一些较低级别的Migrations组件来做到这一点:

You can leverage some of the lower-level Migrations components to do that:

var migrationsAssembly = db.GetService<IMigrationsAssembly>();
var differ = db.GetService<IMigrationsModelDiffer>();

var hasDifferences = differ.HasDifferences(
    migrationsAssembly.ModelSnapshot.Model,
    db.Model);

Assert.False(hasDifferences, "You forgot to add a migration!");

这篇关于检查应用的迁移是否与DbContext相匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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