Resetting Entity Framework Database Migrations

Introduction

When developing .NET apps using the Entity Framework ORM (Object-Relational Mapper) you often find yourself in an intimidating situation with database migrations, it usually become hard to update the database with new migrations or even to roll it back to a previous state.

Many developers try to fix the corrupted migrations by manually editing them but that doesn't work except in some simple situations so if you are in a situation where you can't apply new migrations nor roll back your database state to a good state just continue with this tutorial where I show you the exact steps i usually follow to clean up and reset database migrations.

  • Step 1:

Head over to the migrations folder in your project and delete all *.cs migrations files.

  • Step 2:

Next you need to remove the _MigrationHistory database table where Entity Framework stores the history of previously applied migrations.

  • Step 3:

Run Enable-Migrations command in your Package Manager Console.

  • Step 4:

Run Add-migration Initial in your Package Manager Console. This will create a new migration file which contains the code to create database tables among other things.

  • Step 5:

Now you need to create a corresponding migration entry so head over to your initial migration file then remove the code inside Up method or just comment it because you'll need it later.

Next run Update-database command in your Package Manager Console.

So why removing the code inside Up() method?

Simpy because there are no changes to make, the described changes in Up() method are already applied in the current state of the database such as creating the existing database tables, re-executing them will trigger errors but if we remove them we only have an empty migration file that does nothing except adding a record in the database migration history table.

  • Step 6:

Remove the comments in the initial migration Up method.

So have we done?

We simply removed the migrations files and their related history in database then created an initial migration for the current database state but only after re-enabling the migrations again in our project. Then created an entry in the new _MigrationHistory table by running Update-database command with a do nothing migration file.

Conclusion

Migrations are very helpful, they allow you to upgrade your database schema without dropping the whole database or losing your data, they also allow you to use version control tools such as Git to keep track of your database state changes which is a very beneficial especially if working in teams. But they also have many problems particularly when you are still developing your application so you'll often find yourself looking for secure ways to reset them so i wish this tutorial has helped you if you have problems with your migrations in Entity Framework ORM.

Note: We also publish our tutorials on Medium and DEV.to. If you prefer reading in these platforms, you can follow us there to get our newest articles.

You can reach the author via Twitter:

About the author

Ahmed Bouchefra
is a web developer with 5+ years of experience and technical author with an engineering degree on software development. You can hire him with a click on the link above or contact him via his LinkedIn account. He authored technical content for the industry-leading websites such as SitePoint, Smashing, DigitalOcean, RealPython, freeCodeCamp, JScrambler, Pusher, and Auth0. He also co-authored various books about modern web development that you can find from Amazon or Leanpub


Get our Learn Angular 8 in 15 Easy Steps ebook in pdf, epub and mobi formats, plus a new Angular 8 tutorial every 3 days.


Online Courses (Affiliate)

If you prefer learning with videos. Check out one of the best Angular courses online
Angular 8 - The Complete Guide (2019+ Edition)

Angular Crash Course for Busy Developers

comments powered by Disqus
DMCA.com Protection Status