Removing code first migration and degrade to older state

ef-migrations entity-framework entity-framework-6.1

Question

I have multiple DBContext so I created multiple Code first Migration folders. I just created another DBContext and related table in the code. I also added migration folder and applied in database as InitialSetup. Now I realize that I don't need new changes in DB and code. I want to remove this Migration folder and revert the database changes that were applied during this Migration.

When I am running command like this:

Update-Database –TargetMigration: ContentAddDownloadPath

get this error: More than one migrations configuration type was found in the assembly 'DnpMVCSite'. Specify the name of the one to use.

When specify the Configuration Path:

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

it show the message that "Target database is already at version 201407140515121_ContentAddDownloadPath."

My problem is that I want to remove newly added Migration folder and DB modification done by this migration.

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

Accepted Answer

If you just run following command on Package Manager Console

Add-Migration Step3
Update-Database

And you want to rollback it, just run following command

Update-Database -TargetMigration Step2

Step2 is the name of the migration before Step3.

Then you can remove the Step3 file and also revert the code changes.

Please check this article as reference on paragraft Migrate to a Specific Version (Including Downgrade).

UPDATE

I think what you wanna do is reverting all changes in a specific target migrations folder / target configuration. To do so, you can by specifying -TargetMigration 0.

Be cautious that if you do that, all migrations in that directory / configuration will be reverted even the initial state. For example I have these classes that refer to 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 looks like this

Migration Folder

And if I want to revert all changes in the Migrations2 folder, what I need to do is.

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

Result would be as follow, the city table has been dropped now.

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