简体   繁体   English

EF Add-Migration命令快照提供了备用方案的更改

[英]EF Add-Migration Command Snapshot Provides Redendant Schema Change

I am running a theoretically simple migration. 我正在进行理论上简单的迁移。 For my up() method: 对于我的up()方法:

    DropIndex("dbo.tableA", "IX_index_1");
    AddColumn("dbo.tableB", "column1", c => c.Int(nullable: false));
    DropColumn("dbo.tableA", "column2");

However, upon running the migration, I receive this error in my console: 但是,在运行迁移时,我在控制台中收到此错误:

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

When I run the 当我运行

    Add-Migration

The 'snapshot' provides the same schema changes. “快照”提供了相同的架构更改。 What is the reason for this?? 这是什么原因? Thank You. 谢谢。

This can happen if two people create migrations at the same time and check them in independently of each other. 如果两个人同时创建迁移并相互独立签入,则会发生这种情况。

So say you have 3 migrations A,B,C checked into source control. 假设您有3个迁移A,B,C已签入源代码管理。 Two people get them and create migrations D and E. Both D and E have a snapshot of the schema that includes changes up to C. The person who created D checks in and then the person who created E gets a latest. 两个人得到了它们,并创建了迁移D和E。D和E都具有包含直到C的更改的架构的快照。创建D的人签入,然后创建E的人得到最新的。 EF will think the model is out of sync because the snapshot in E does not include D. When you add the migration, it calculates all the changes from AE including D and sees there is nothing new to do so there is nothing in the migration. EF会认为该模型不同步,因为E中的快照不包括D。添加迁移时,它会计算AE中包括D在内的所有更改,并且发现没有新操作可做,因此迁移中没有任何内容。

The snapshot is stored in a resx file under the migration. 快照存储在迁移下的resx文件中。 If you open it in the xml editor you will see the snapshot in there. 如果在xml编辑器中将其打开,则会在其中看到快照。

The way to fix this would be to keep the empty migration since it doesn't do any harm, or the person who did E could delete and recreate their migration so that it's snapshot includes the changes from D. 解决此问题的方法是保留空迁移,因为它不会造成任何危害,或者执行E的人员可以删除并重新创建其迁移,以便其快照包括D所做的更改。

Hope that helps! 希望有帮助!

I wanted to add what I and my team ultimately ended up choosing. 我想补充一下我和我的团队最终选择的内容。

Although I selected MikeS's answer as the chosen answer, another solution we found was to roll back to the last stable migration before the branches began interfering with one another, delete the newer migrations, and then consolidating the problematic migrations. 尽管我选择了MikeS的答案作为选择的答案,但我们发现的另一种解决方案是在分支开始相互干扰之前回退到上一个稳定的迁移,删除较新的迁移,然后合并有问题的迁移。

The Add-Migration command automatically picked up on the needed schema changes, then we added our logic for transferring records. Add-Migration命令自动获取所需的架构更改,然后添加了用于传输记录的逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM