Reset Entity-Framework Migrations

2019-01-01 07:44发布

问题:

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?

回答1:

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
    


回答2:

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.



回答3:

How about

Update-Database –TargetMigration: $InitialDatabase

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)



回答4:

To fix this, You need to:

  1. Delete all *.cs files in the Migrations Folder.

  2. Delete the _MigrationHistory Table in the Database

  3. Run Enable-Migrations -EnableAutomaticMigrations -Force

  4. Run Add-Migration Reset

Then, in the public partial class Reset : DbMigration class, you need to comment all of the existing and current Tables:

public override void Up()
{
// CreateTable(
// \"dbo.<EXISTING TABLE NAME IN DATABASE>
// ...
// }
...
}

If you miss this bit all will fail and you have to start again!

  1. Now Run Update-Database -verbose

This should be successful if you have done the above correctly, and now you can carry on as normal.



回答5:

My problem turned out to be that I manually removed the Migrations folder. I did that because I wanted to back up the contents, so I simply dragged the folder out of the project. I later fixed the problem by putting it back in (after making a backup copy), then removing the Migrations folder by right-clicking it in Solutions Explorer and choosing Delete from the popup menu.



回答6:

In EntityFramework 6 please try:

Add-Migration Initial

in order to update the initial migration file.



回答7:

In EF6

  1. Delete all your files in \'migrations\' folder... But not the \'initial create\' or \'config\'.
  2. Delete the database.
  3. Now run Add-Migration Initial.
  4. Now you can \'update-database\' and all will be well.


回答8:

In Entity Framework Core.

  1. Remove all files from migrations folder.
  2. Type in console

dotnet ef database drop -f -v

dotnet ef migrations add Initial

dotnet ef database update



回答9:

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:

  1. Delete the _efmigrationhistory table.
  2. Search for your entire solution for files that contain Snapshot in their name, such as ApplicationDbContextSnapshot.cs, and delete them.
  3. Run 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



回答10:

This method doesn\'t require deleting of the __MigrationHistory table, so you don\'t have to put your hands on the database on deploy.

  1. Delete existing migrations from the Migrations folder.
  2. In Package Manager Console run Add-Migration ResetMigrations
  3. Clean migration history in the Up() method:
/// <summary>
/// Reset existing migrations by cleaning the __MigrationHistory table
/// and creating a new initial migration with the current model snapshot.
/// </summary>
public partial class ResetMigrations : DbMigration
{
    public override void Up()
    {
        Sql(\"DELETE FROM [dbo].[__MigrationHistory]\");
    }

    public override void Down()
    {
    }
}


回答11:

Enable-Migrations -EnableAutomaticMigrations -Force