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
?
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:
Delete existing migrations from Migrations_History table.
Delete existing migrations from the Migrations Folder.
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.)
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.
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.
To fix this, You need to:
Delete all *.cs files in the Migrations Folder.
Delete the _MigrationHistory Table in the Database
Run
Enable-Migrations -EnableAutomaticMigrations -Force
Run
Add-Migration Reset
Then, in the
public partial class Reset : DbMigration
class, you need to comment all of the existing and current Tables:If you miss this bit all will fail and you have to start again!
Update-Database -verbose
This should be successful if you have done the above correctly, and now you can carry on as normal.
In Entity Framework Core.
Considering this still shows up when we search for EF in .NET Core, I'll post my answer here (Since it has haunted me a lot). Note that there are some subtleties with the EF 6 .NET version (No initial command, and you will need to delete "Snapshot" files)
(Tested in .NET Core 2.1)
Here are the steps:
_efmigrationhistory
table.ApplicationDbContextSnapshot.cs
, and delete them.Add-Migration InitialMigration
Please note: You must delete ALL the Snapshot files. I spent countless hours just deleting the database... This will generate an empty migration if you don't do it.
Also, in #3 you can just name your migration however you want.
Here are some additional resources: asp.net CORE Migrations generated empty
Reset Entity Framework 7 migrations
How about
in Package Manager Console? It should reset all updates to its very early state.
Reference link: Code First Migrations - Migrating to a Specific Version (Including Downgrade)