简体   繁体   English

EF迁移没有待处理的更改,但仍然抱怨有待更改。 EF如何跟踪这一点?

[英]There are no pending changes for EF migration and still it complains that there are pending changes. How does EF keep track of this?

I have several migration files in my project and since I made manual modifications to my last migration I don't want to re-generate it with the "package manager console". 我的项目中有几个迁移文件,因为我对上次迁移进行了手动修改,所以我不想使用“包管理器控制台”重新生成它。 I only need to add 1 column. 我只需要添加1列。 So Added this manually a previous migration (I can do this since no-one has upgraded yet). 所以手动添加了以前的迁移(我可以这样做,因为还没有人升级)。

Now, when I start my project, the local database seems to create my new column fine but I do get an exception: "Unable to update database to match the current model because there are pending changes and automatic migration is disabled" 现在,当我启动我的项目时,本地数据库似乎创建了我的新列,但我确实得到了一个例外:“无法更新数据库以匹配当前模型,因为存在挂起的更改并且禁用了自动迁移”

It looks like the only way I can solve this is to generate an extra migration - even though, this migation generates exactly the same line of code that i wrote manually in a previous migration... 看起来我能解决这个问题的唯一方法是生成一个额外的迁移 - 尽管这个migation生成的代码行完全与我在之前的迁移中手动编写的代码相同...

I was wondering - how does EF keep track of this and is there a way to bypass it ? 我想知道 - EF如何跟踪这个并且有没有办法绕过它?

Also another question - is it wrong of me to want to limit the amount of migration files that I have ? 还有一个问题 - 我想要限制我拥有的迁移文件数量是不对的? I currently feel that in an ideal situation each Release of my software should only have 1 migration file at most in order to keep a better overview of my code... 我目前认为在理想的情况下,我的软件的每个版本最多只能有1个迁移文件,以便更好地概述我的代码......

thank you, 谢谢,

EF saves a hash of your serialized Model in the _MigrationHistory table, and compares them when you use migrations to ensure the database schema matches the model. EF将序列化模型的哈希值保存在_MigrationHistory表中,并在使用迁移时对它们进行比较,以确保数据库模式与模型匹配。 I do not advise trying to bypass this. 我不建议试图绕过这个。 If you wish to mimimise the number of files then you can rollback then combine the migrations. 如果您希望最大限度地减少文件数量,则可以回滚然后组合迁移。 But I don't think it's worth it. 但我不认为这是值得的。 I just put my migrations into sub-folders periodically 我只是定期将我的迁移放入子文件夹中

I recommend this article: 我推荐这篇文章:

http://elegantcode.com/2012/04/12/entity-framework-migrations-tips/ http://elegantcode.com/2012/04/12/entity-framework-migrations-tips/

Check the table called _MigrationHistory in your Database. 检查数据库中名为_MigrationHistory的表。 This will have the history of migrations you ran. 这将包含您运行的迁移历史记录。

I don't think single migration is such a good idea. 我认为单一迁移不是一个好主意。 Because: 因为:

  • Each migration is like a version of the Database. 每次迁移都像数据库的一个版本 You can go back to any version by doing "update-database -target migration MigrationName". 您可以通过执行“update-database -target migration MigrationName”返回任何版本。

  • If many people are working on the project, it will become very difficult to keep track of what version is your DB is in and it will get messy. 如果很多人正在研究这个项目,那么跟踪你的数据库所处的版本就会变得非常困难,而且会变得混乱。

If you want to add an extra column you can - force it to previous migration. 如果要添加额外的列,可以强制执行以前的迁移。 Otherwise it is better to have multiple migration to avoid the confusion. 否则,最好进行多次迁移以避免混淆。

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

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