多个开发人员同时签入迁移时的 EF 迁移

EF migrations when several developers checks in migrations at the same time

当 2 个开发人员同时处理一个项目时,我有以下情况。

我创建了一个table,使用了"add-migration, update-database"。 #2 创建一个 table,使用 "add-migration, update-database"。

然后我们都检查对 GIT 的更改(我先)。

当我执行更新数据库时,我得到 "Unable to update database to match the current model because there are pending changes..."

然后我执行 "Add-Migration" 并创建迁移,尝试创建我在上面第一步中创建的 tables。当我尝试使用此迁移执行 "Update-Database" 时,我当然会得到 "There is already an object named ... in the database"。

目前我找到的解决方法是:

  1. 更新数据库-TargetMigration MyLastmigration
  2. 删除 #2 所做的迁移
  3. 添加迁移 theSameNameAs#2Made
  4. 更新数据库

我是不是做错了什么? #2 做错了什么吗?我的解决方法对#2 有效吗?

有两种方法可以解决这个问题。

  1. 添加空白合并迁移如下:

    Add-Migration Merge –IgnoreChanges

    此命令将创建一个没有任何脚本的新迁移,但会 更新模型快照以匹配当前模型。

  2. 更新上次迁移中的模型快照 - 您拥有的方法 提到,这消除了创建额外的空白迁移的需要 选项1中提到。

以上两种方法都行,没有错。阅读这篇 MSDN 文章了解详细说明