合并多个迁移实体框架代码优先

本文关键字:代码 框架 实体 迁移 合并 | 更新日期: 2023-09-27 18:02:26

我有以下场景-

我在一个分支机构工作,在那里我添加了3个新的迁移-根据惯例,我将把它们命名为

Migration A
Migration B
Migration C

当尝试与master合并时,发现有4个新的迁移没有按时间顺序排列(从另一个分支合并(。我的意思是以下

new Migration from master 1
existing Migration in both branches
existing Migration in both branches
new Migration from master 2
new Migration from master 3
new Migration from master 4
existing Migration in both branches
existing Migration in both branches

合并时,我得到以下场景

new Migration from master 1
existing Migration in both branches
existing Migration in both branches
new Migration from master 2
new Migration from master 3
new Migration from master 4
existing Migration in both branches
existing Migration in both branches
Migration A
Migration B
Migration C

现在很明显,Entity Framework不允许我更新数据库,因为它认为模型是错误的——当添加新的迁移时,它会从前面提到的master的4个新迁移中生成更改。

真正的问题是,我无法恢复到现有的迁移,然后重新构建脚手架来更新元数据,因为它们没有按时间顺序排列(根据另一个问题中已接受的答案建议(。

我曾尝试将update-database -target恢复到new Migration from master 1之前的迁移,然后再将Add-Migration <timestamp> new Migration from master 1重新构建,但我得到了Unable to generate an explicit migration because the following explicit migrations are pending....异常。

我真的不想添加空白迁移来解决这个问题,而是想找到一个干净好的解决方案。

合并多个迁移实体框架代码优先

经过进一步的研究,解决方案是在最后一次迁移之前将数据库更新为迁移,然后重新构建上一次迁移。

update-database -target Migration B
add-migration <timestamp>_Migration C