I am currently working on a side-project with the code-first approach. I really like how the framework does migrations and you can use the .sql files in the migration folder etc.
But I am still wrapping my head around the model/poco classes I want to use and the data structure is still changing in this stage of development. I have moved from 1-n relations to n-n etc.
I sometimes stumbled on a situation where there were foreign key constraints which made it (almost) impossible to process a migration. I also did not found how to revert one or cancel one if you have a pending one and cannot update the database.
Because I was still starting with the project I could not bother to loose all the data and start over with a clean/empty database. I have read several blogs about how to do this, but I wanted to blog my situation. Perhaps it’s useful for someone, perhaps it’s just an online note to myself.
Here is how I did it:
- Delete the ‘__MigrationHistory’ table
- Delete all files in the ‘Migrations’ folder of your project except the ‘Configuration.cs’
- Delete all other tables (one by one) in the DB
- In the package manager console, select the correct project and ‘Add-Migration <name>’
- Update-Database (this will recreate all tables and use the initial data from the configuration file, if you had any)
A lot of blogposts are based on keeping your data. In that case, you should skip step 3 (obvious) and comment out the ‘Up’ method when you have added your migration and have the generated file open.
This is a slightly modified version of this blogpost which uses the brand new SQL Operations Studio 0.23.6