How to Cleanup & generate fresh migration for existing DB?

code-first ef-code-first ef-migrations entity-framework entity-framework-core

Question

We wish to get rid of 100s of migration classes as DB schema in production is final.

Here are the steps I followed:

  1. Delete Migrations folder.
  2. Add-Migration -??

What command line switches, could help us?

EDIT:

If all goes well Up() method of migration should be empty right? For example following is wrong generation on Add-Migration. Because if we execute the project we will get duplicate table errors.

public partial class Sanity : DbMigration
{
    public override void Up()
    {
        CreateTable(
            "dbo.AccountPreferences",
            c => new
            {
                AccountID = c.Guid(nullable: false),
            }
            .... for 1000s of tables
      }
}

A clean migration would be something: when trying Add-Migration on subsequent changes, should not be getting any error.

Unable to generate an explicit migration because the following explicit migrations are pending: [201712281054591_Sanity]. Apply the pending explicit migrations before attempting to generate a new explicit migration.

As you can see if we happen to execute Update-Database will get table already exist error.

Are we forced to always retains all migration copies?

1
0
12/28/2017 2:22:44 PM

Accepted Answer

See if this can help:

MVC3 and Code First Migrations - "model backing the 'blah' context has changed since the database was created"

Entity framework code first - how to run Update-Database for production database

How to delete and recreate from scratch an existing EF Code First database


note:
I'm writing this from memory, if you have issues let me know and I'll recheck exactly.
Also, my knowledge on this is from slightly older versions of EF as I haven't done much work there recently, but I doubt much has changed.

From what I can tell, if you want to...
a) keep the db,
b) clean your project migrations,
c) have the 2 'match', be in sync:

do the following:
- Remove the migration folder (your project)
- Run Add-Migration Initial - then should add one migration
- caution: it is safe but do backup, change connection string etc. before the next step
- Run Update-Database -Script - that doesn't update the db but creates the SQL script, including the migration table
- find the INSERT INTO [__MigrationHistory] records, just run those (on your db), insert them into the database

...then test with Add-Migration again, to see if it is going to make anything, should yield no new migrations, empty one.

Please read through the first link above and adjust approach as needed.

I'm sure there might be easier, shorter ways to do this (via PM console) but unaware of it at the moment.

1
12/28/2017 2:20:43 PM

Popular Answer

Open your database.

Clear table __MigrationHistory

Remove migrations in the folder

Run Add-Migration MigrationName



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow