Removing code first migration and degrade to older state

ef-migrations entity-framework entity-framework-6.1

Question

I made numerous Code first Migration folders because I have multiple DBContext. I recently added a new DBContext and associated table to the code. Along with the migration folder, I added an InitialSetup application to the database. I now understand that my database and code don't require any new adjustments. The database modifications that were made during this Migration should be undone, hence I wish to remove the Migration folder.

When I issue the following command:

Update-Database –TargetMigration: ContentAddDownloadPath

receive this error In the assembly "DnpMVCSite," more than one migrations configuration type was discovered. Name the person you want to use.

Set the Configuration Path as follows:

Update-Database -ConfigurationType DnpMVCSite.Migrations.ContentMigration.Configuration –TargetMigration: ContentAddDownloadPath

Target database is already at version 201407140515121 ContentAddDownloadPath, according to the notice that appears.

The issue I'm having is that I want to delete the newly added Migration folder and the DB modification that this migration made.

1
3
7/28/2014 5:54:23 AM

Accepted Answer

Run the command below on the Package Manager Console.

Add-Migration Step3
Update-Database

If so, use the following command to roll it back.

Update-Database -TargetMigration Step2

Step2 is the title of the earlier migration.Step3 .

Next, you can take away theStep3 Revert the code modifications and the file as well.

Please use the paragraft Upgrade to a Particular Version (Including Downgrade) as a reference by looking at this piece.

UPDATE

Reverting all modifications to a certain target migrations folder or target configuration is what I believe you want to do. You can accomplish this by declaring-TargetMigration 0 .

Be aware that if you do that, any migrations in that directory or configuration—including the initial state—will be undone. As an illustration, I have these classes that use the same database.

public class AppContext : DbContext
{
    public DbSet<Customer> Customers { get; set; }
}
public class Customer
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Code { get; set; }
    public string Area { get; set; }
}
public class AppContext2 : DbContext
{
    public AppContext2()
        : base("AppContext")
    {
    }
    public DbSet<City> Cities { get; set; }
}
public class City
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Code { get; set; }
    public string Area { get; set; }
    public string Grade { get; set; }
    public string Division { get; set; }
}

My directory appears as follows.

Migration Folder

And this is what I must do if I want to undo all of the modifications to the Migrations2 folder.

Update-Database -ConfigurationTypeName PlaygroundTestEF.Migrations2.Configuration -TargetMigration 0

The city table has now been dropped as a consequence.

End Result

9
7/28/2014 7:22:09 AM


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