Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you downgrade a Entity Framework 5 migration in Visual Studio 2012?

I've noticed that when I create a code first database migration using add-migration it generates a Down() method as well as an Up() method.

How do I tell my database to downgrade?

like image 215
Jamie Street Avatar asked Nov 25 '13 22:11

Jamie Street


People also ask

How do I downgrade Entity Framework migration?

We can use the –TargetMigration switch to downgrade to this migration. Run the Update-Database –TargetMigration: AddBlogUrl command in Package Manager Console. This command will run the Down script for our AddBlogAbstract and AddPostClass migrations.

How do I change Entity Framework version?

Right-click on your project in Solution Explorer and select Manage NuGet Packages... Select Updates. Select EntityFramework (make sure it is going to update it to the version you want) Click Update.

Which command will rollback to a given migration if already applied to the database?

In this case, use the update-database <migration name> command to revert the database to the specified previous migration snapshot. >


2 Answers

After almost giving up with researching on Google I managed to find this quote from here:

  • http://msdn.microsoft.com/en-gb/data/jj591621.aspx#specific

Which Specifies:

Let’s say we want to migrate our database to the state it was in after running our AddBlogUrl migration. We can use the –TargetMigration switch to downgrade to this migration.

Run the Update-Database –TargetMigration: AddBlogUrl command in Package Manager Console. This command will run the Down script for our AddBlogAbstract and AddPostClass migrations.

If you want to roll all the way back to an empty database then you can use the Update-Database –TargetMigration: $InitialDatabase command.

like image 90
Jamie Street Avatar answered Sep 17 '22 13:09

Jamie Street


First get the name of the migration that was applied before the one you want to downgrade by issuing the Get-Migrations command.

PM> Get-Migrations Retrieving migrations that have been applied to the target database. 201508242303096_Bad_Migration 201508211842590_The_Migration_applied_before_it 201508211440252_And_another 

This list shows the migrations listing the most recent applied migration first. Pick the migration that occurs in the list after the one you want to downgrade, ie the one applied before the one you want to downgrade.

Update-Database –TargetMigration: "<the migration applied before it>" 

All migrations applied after the one specified will be down-graded in order starting with the latest migration applied first.

like image 22
David Sopko Avatar answered Sep 19 '22 13:09

David Sopko