即使禁用了自动迁移,Update-Database也会尝试进行自动迁移 [英] Update-Database tries to do an automatic migration even with automatic migrations disabled

查看:145
本文介绍了即使禁用了自动迁移,Update-Database也会尝试进行自动迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我组成一个由4个开发人员组成的团队,他们使用EF5,每个人都在自己的本地数据库上工作。到目前为止,我们一直在使用自动迁移,但是我们已经接近需要发布到生产环境的地步,因此我们禁用了自动迁移并开始添加基于代码的显式迁移。

I work on a team of 4 developers using EF5, everyone working on their own local database. Up until now we've been using automatic migrations but we're nearing the point where we need to release to production so we've disabled automatic migrations and started adding explicit code-based migrations.

这是问题所在:开发人员创建新的显式迁移后,我运行了 Update-Database 命令,并且出现以下错误:

Here is the problem: I ran the Update-Database command after a developer created a new explicit migration and I get the following error:

Applying code-based migrations: [201209080142319_CreatedDate.LastModifiedDate.Additions].
Applying code-based migration: 201209080142319_CreatedDate.LastModifiedDate.Additions.
Applying automatic migration:    201209080142319_CreatedDate.LastModifiedDate.Additions_AutomaticMigration.
Automatic migration was not applied because it would result in data loss.

即使禁用了自动迁移,为什么仍会出现此错误?我可以通过删除显式迁移并重新构建它(运行 Add-Migration )来解决此错误。然后 Update-Database 运行良好,并且没有提及有关自动迁移...的任何内容。此外,我在运行Add-Migration时在迁移中创建的代码是与我的队友创建的相同。我不明白为什么它甚至会尝试进行自动迁移,因为 AutomaticMigrationsEnabled = false;

Why do I get this error even though I've disabled automatic migrations? I can fix this error by deleting the explicit migration and then re-scaffolding it (running Add-Migration). Then Update-Database runs fine and doesn't mention anything about 'Automatic migration...' Also, the code in the migration created by me when I run Add-Migration is identical to the one created by my teammate. I don't see why it would even try to do an automatic migration since AutomaticMigrationsEnabled = false;.

什么我在这里想念吗?

推荐答案

我不想回答自己的问题,但是又遇到了这个问题。我们团队中的开发人员在其本地计算机上重新启用了自动迁移,然后创建了一个明确的迁移,该迁移在我运行该行为后便立即重现了这种行为。

I hate to answer my own question but I encountered this problem again. A developer on my team re-enabled automatic migrations on their local machine and then created an explicit migration, which reproduced this behavior as soon as I ran it.

实体框架将始终运行自动迁移,然后再运行显式迁移,该显式迁移的 .resx Source 属性。 c $ c>文件,即使 AutomaticMigrationsEnabled = false 。如果显式迁移是在运行自动迁移之后创建的,则只会设置 Source 属性。

Entity framework will always run an automatic migration before it runs an explicit migration that has the Source property defined in its .resx file, even if AutomaticMigrationsEnabled = false. An explicit migration will only have the Source property set if it is created after an automatic migration has been run.

结果是,禁用自动迁移仅意味着EF在检测到模型更改时不会自动升级架构-但如果它需要填补某些显式迁移之间的空白,它可能仍会进行自动迁移。为避免这种情况,请勿同时使用自动迁移和显式迁移。

The upshot is that disabling automatic migrations only means that EF won't automatically upgrade your schema when it detects model changes - but it might still do an automatic migration if it needs to fill in a gap between some explicit migrations. To avoid this behavior, don't use a mixture of automatic migrations and explicit migrations.

这篇关于即使禁用了自动迁移,Update-Database也会尝试进行自动迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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