简体   繁体   中英

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. 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...

I was wondering - how does EF keep track of this and is there a way to bypass it ?

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...

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. 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/

Check the table called _MigrationHistory in your Database. 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".

  • 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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