Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How the right way to rollback migration with Entity Framework

I'm very newbie with EF and migration. I'm trying to make a rollback with the command, to run the Down method

update-database -TargetMigration MyLastMigration

Output result

Target database is already at version 201701031905415_MyLastMigration.

How can I execute the Down method of MyLastMigration?

Thanks in advance

like image 584
Vinicius Maciel Avatar asked Jan 03 '17 19:01

Vinicius Maciel


People also ask

How do I rollback migration?

You can rollback your migration by using rake db:rollback with different options. The syntax will be different according to your requirements. where n is number of migrations to rollback, counting from latest migration. where xxxxx is the version number of the migration.

How do I get rid of old migration?

Delete your Migrations folder. Create a new migration and generate a SQL script for it. In your database, delete all rows from the migrations history table. Insert a single row into the migrations history, to record that the first migration has already been applied, since your tables are already there.

How do I run an existing migration in Entity Framework?

Run the Add-Migration InitialCreate command in Package Manager Console. This creates a migration to create the existing schema. Comment out all code in the Up method of the newly created migration. This will allow us to 'apply' the migration to the local database without trying to recreate all the tables etc.


1 Answers

Your target migration should be the migration immediately previous to the one that you want to rollback:

update-database -SourceMigration MyLastMigration -TargetMigration MigrationPreviousToMyLastMigration

The SourceMigration parameter is optional in your case, as you have not applied any migration after MyLastMigration.

To check the name of the previous migration you can use Get-Migrations, that returns the list of migrations applied to your database.

Edit: as Ivan Stoev says in the comments, the SourceMigration parameter can only be included together with the parameter Script, so it does not make sense in this scenario. The right command would be like this:

update-database -TargetMigration MigrationPreviousToMyLastMigration
like image 56
Diana Avatar answered Sep 20 '22 00:09

Diana