Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reset Entity-Framework Migrations

I've mucked up my migrations, I used IgnoreChanges on the initial migration, but now I want to delete all my migrations and start with an initial migration with all of the logic.

When I delete the migrations in the folder and try and Add-Migration it doesn't generate a full file (it's empty - because I haven't made any changes since my last, but now deleted, migration).

Is there any Disable-Migrations command, so I can rerun Enable-Migrations?

like image 881
Kind Contributor Avatar asked Jul 26 '12 23:07

Kind Contributor


People also ask

How do you reset migrations?

Reset the Whole Database in Djangosqlite3 and then delete all the migrations folders inside all the apps. After deleting the migrations folders, we can remake the migrations and migrate them using two commands; namely, python manage.py makemigrations and python manage.py migrate .

How do I revert my ef core migration?

To revert the last applied migration you should (package manager console commands): Revert migration from database: PM> Update-Database <prior-migration-name> Remove migration file from project (or it will be reapplied again on next step) Update model snapshot: PM> Remove-Migration.


2 Answers

You need to :

  1. Delete the state: Delete the migrations folder in your project; And
  2. Delete the __MigrationHistory table in your database (may be under system tables); Then
  3. Run the following command in the Package Manager Console:

    Enable-Migrations -EnableAutomaticMigrations -Force 

    Use with or without -EnableAutomaticMigrations

  4. And finally, you can run:

    Add-Migration Initial 
like image 72
Kind Contributor Avatar answered Oct 09 '22 09:10

Kind Contributor


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.

like image 37
Greg Gum Avatar answered Oct 09 '22 08:10

Greg Gum