Reset Entity Framework migrations
The fine art of starting over with code first migrations
Code first migrations for Entity Framework are great but migrations add up over time. Sometimes you really need to clean them out to keep on top of what's going on. Sometimes you just wish you could start fresh.
Be advised that the solution you will see in this post will be irreversible - Please use source control and back up your database.
With the disclaimer out of the way - let's get to it
1: Delete your _MigrationHistory table
You may never have noticed this table in your database but it's there. It is used for storing meta data about your migrations and if we want to start over we need it gone.
2: Delete your migration files
Locate your migration files in the solution explorer - There is one for each migration you have done.
3: Add-Migration Initial
With all migration files and metadata gone it's now time to start over. Open up your Package Manager Console and add your initial migration by running the command
Add-Migration Initial
You can name it whatever you want but initial seems to fit the purpose.
4:Fake Update-database
Now we have our migration but we like the database just the way it is. That means that we don't want to actually run the migration. However we need for code first migrations to believe that we did so that we can move on.
Go into your new migration file and comment out the code inside the Up() method. That way we can run an update-database without any sql-updates being run.
Now run update-database command
Update-database
Uncomment the contents of Up()
Ba da bing ba da boom - you're now started over on code first migration with you whole model in one nice migration file
You might also be interested in these articles...