Reset Entity Framework Migrations and Drop Database

Last Updated on August 21, 2022 by Pupli

Remove all files from the migrations folder.

dotnet ef database drop -f -v
dotnet ef migrations add Initial
dotnet ef database update

Do that only if you don’t care about your current persisted data

Without Dropping Database

The Issue: You have mucked up your migrations and you would like to reset it without deleting your existing tables.

The Problem: You can’t reset migrations with existing tables in the database as EF wants to create the tables from scratch.

What to do:

  1. Delete existing migrations from Migrations_History table.
  2. Delete existing migrations from the Migrations Folder.
  3. Run add-migration Reset. This will create a migration in your Migration folder that includes creating the tables (but it will not run it so it will not error out.)
  4. You now need to create the initial row in the MigrationHistory table so EF has a snapshot of the current state. EF will do this if you apply a migration. However, you can’t apply the migration that you just made as the tables already exist in your database. So go into the Migration and comment out all the code inside the “Up” method.
  5. Now run update-database. It will apply the Migration (while not actually changing the database) and create a snapshot row in MigrationHistory.

You have now reset your migrations and may continue with normal migrations.

References
https://stackoverflow.com/questions/11679385/reset-entity-framework-migrations