Reputation: 974
I admit that the source of this problem is probably on me, but I cannot get Entity Framework to "recover". The situation is that I'm creating new migration and Entity Framework is not detecting all the changes of my model. It detects some (the latest), but not the previous ones (with no migration either).
Why it is like this, is that I first made some changes to the model and created a migration. All was perfect. Then I changed the model again and since it was all on my dev machine only, I deleted the previous migration and recreated it. This is where the problems started. I know that I shouldn't have deleted a migration at all, but what's done is done.
I have code first database, so I decided to nuke the DB (LocalDB), nuke the latest migration files (that didn't have all the changes). Then update the database and try to add new migration. Still not all the changes.
So I'm thinking that EF is keeping some kind of snapshot of the model somewhere where I cannot see it. Where? Any ideas how to recover from this situation and get EF to re-create the migration with the missing changes to the model?
Upvotes: 1
Views: 7305
Reputation: 89
you should insert this code line into OnModelCreating method
modelBuilder.ApplyConfigurationsFromAssembly();
Upvotes: 0
Reputation: 141
I am using Entity Framework Core, and I got into this situation after Update-Database to a previous migration, and deleting several migrations (intending to consolidate a batch of changes I made while problem soving). Clearing all the /bin /obj directories didn't help, nor did cleaning.
Eventually I discovered that it updates the file {your context name}ModelSnapshot.cs. I reverted all changes to that file, and EF Core finally discovered all my model changes the next time I ran Add-Migration.
Upvotes: 4
Reputation: 9463
As you already have discovered, EF keeps a Hashcode to track the model changes between the migrations. You can see this by looking at the [dbo].[__MigrationHistory]
table in your database.
So you should not just simply delete the .cs files of a migration that has already been applied. Instead do the following:
1) Rollback old migration using Update-Database -TargetMigration [Name of the migration right before the migration you want to delete]
2) Delete old migration .cs files
3) Add new migration using Add-Migration
I hope you still have the deleted migration .cs files somewhere, else the rollback will prove problematic :/ If you start with a fresh database, only update it to the last good migration (using the -TargetMigration
parameter again) and start from there.
Upvotes: 2