c# - 插入迁移

标签 c# entity-framework ef-code-first entity-framework-6 entity-framework-migrations

故事是这样的。我们在使用 Entity Framework Code First 迁移的源代码控制中有一个产品。为了这个问题,有两个分支,开发和生产。目前开发分支有几个不在生产分支中的迁移,但它们是从生产分支中的最后一个迁移开始的。

现在,我们需要对生产分支进行一个小的架构更改。我们通过添加迁移来做到这一点。然后将其发布到生产站点。

那么,我们如何将这些变更集成到开发分支中呢?开发分支中的迁移必然是生产站点上的迁移的后代,但我们只是在生产站点上插入了一个新的迁移。这意味着当我们合并到开发分支时,迁移会失败,因为存在乱序迁移。

那么,在我的开发分支中,是否有任何方法可以从另一个分支插入迁移,从而将现有的迁移分流到列表中?

最佳答案

我在合并来自不同开发人员的更改时处理过这个问题。在为这个答案做一些研究时,我遇到了 this handy article这可能比我能更好地解释它。 las,你被我困住了。有几点需要注意:

  1. Entity Framework 不会每次都直接将数据库与您的类进行比较。相反,它比较它生成的 edmx 模型。

  2. 因此,您在尝试更新时会收到警告

Unable to update database to match the current model because there are pending changes and automatic migration is disabled…

尽管它实际执行更新。错误是指它比较的快照(?)edmx

最简单的方法是添加一个空白的“合并迁移”。以下是文章中的步骤:

  1. Ensure any pending model changes in your local code base have been written to a migration. This step ensures you don’t miss any legitimate changes when it comes time to generate the blank migration.

  2. Sync with source control.

  3. Run Update-Database to apply any new migrations that other developers have checked in.

    Note:if you don’t get any warnings from the Update-Database command then there were no new migrations from other developers and there is no need to perform any further merging.

  4. Run Add-Migration [pick_a_name] –IgnoreChanges (e.g. Add-Migration Merge –IgnoreChanges). This generates a migration with all the metadata (including a snapshot of the current model) but will ignore any changes it detects when comparing the current model to the snapshot in the last migrations (meaning you get a blank Up and Down method).

关于c# - 插入迁移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33763022/

相关文章:

c# - 我应该覆盖字符串转换还是 ToString 方法?

c# - 在 VS2015 中运行第一个单元测试的启动时间

c# - EF Code First 6.1 将 ID 从 Int 更改为 Long

.net - 在不同的TFS分支上工作时丢失EF代码优先迁移?

entity-framework - EF Code First + 延迟加载,投影没有按预期工作

c# - 具有作为通用引用接口(interface)的值的通用字典

c# - 如何将列表转换为 ienumerable

.net - 带有 Entity Framework 的存储库模式

c# - 在 Linq Select 中创建元组

c# - "EntityType has no key defined"异常,尽管键是用 HasKey 定义的