I am not even sure how to phrase this one. I have migrations applied to the db: Migration One. Migration Two. Migration Three. I just pulled a "Migration 2.5" - if I try to revert to migration Two and `Update-Database', the db updates successfully and says there are pending changes. These "pending changes" are Migration 2.5, even though it's been applied - EF wants me to add them again.
Now, normally, I get around the problem like this - revert to Migration One or Two, delete Migration 2.5, apply changes, and create a new "Migration 4" that comes LAST. This way the problem is avoided. However, I cannot change around the order of the migrations now. How do I solve this?
Edit: I think it's important to note that my db does work properly. If there is a db/code mismatch, EF throws an error stating so - this isn't happening. The problem is that the moment I do need to Add-Migration
with a legitimate change, it will try to duplicate the "2.5" changes along with the actual legitimate changes.
This isn't really an answer to the original question - I have not found a way to make EF recognize that it has applied an "out of order" migration. However, here are two ways to get around the problem, which, more specifically, is getting the quoted error when trying to pull an "out of order" migration:
Unable to update database to match the current model because there are pending changes and automatic migration is disabled. Either write the pending model changes to a code-based migration or enable automatic migration. Set DbMigrationsConfiguration.AutomaticMigrationsEnabled to true to enable automatic migration.
Create an empty "Fix" migration - this is a fairly simple, but dirty solution. You simply run Add-Migration
. You then delete migration actions, so you should be left with empty blocks like so:
public override void Up()
{
}
public override void Down()
{
}
You then run Update-Database
. The reason this works is simple - as pointed out earlier, EF the "out of order" migration was not applied, despite the fact that it was actually applied the first time you ran Update Database
- note that you should be able to verify that it was applied with Get-Migrations
. Anyway, for some reason EF wants to duplicate it, but will be placated by an empty migration instead. Makes no sense to me.
This solution is undesirable as it leaves a pointless migration, but may be your only route if you cannot easily get to the environments which the out of order migration has already been pushed to.
a. Update-Database Target-Migration "The Migration Before The Out of Order One"
to revert to the migration before this problem began.
b. Now manual delete the "Out of Order" migration file from your migrations folder.
c. Run Update-Database
again to bring your db current with all changes EXCEPT the "Out of Order" migration.
d.Run Add-Migration
- this will recreate the "Out of Order" migration you deleted in the beginning, but in its proper,or at least functional, place.
e. Run Update-Database
and you are done.
Note that this will create problems unless synchronized with your team for reasons I am too lazy to go into. #2 is better, but #1 is easier and faster.
P.S. Stackoverflow nested list syntax is too much for me to handle.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With