Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF 4.3 Migration - how to produce a downgrade script?

I have an issue which I could not find answer for across the web.

I am using CodeFirst EF 4.3.1 Migrations with MsSQL.

I have added several migrations and now I want to produce a script for upgrade/downgrade between two migrations.

For upgrade I run the following command which successfully reproduces an upgrade script:

PM> Update-Database -Script -SourceMigration:"201205161144187_AddPostAbstract" -TargetMigration:"201205161203310_BlogLimitsAndTableRename"

However, for downgrade I run the following command which fails with the following error:

PM> Update-Database -Script -SourceMigration:"201205161203310_BlogLimitsAndTableRename" -TargetMigration:"201205161144187_AddPostAbstract"
Scripting the downgrade between two specified migrations is not supported.

Any ideas how can I generate a downgrade script?

Thanks.

like image 317
mayash Avatar asked Jun 03 '12 15:06

mayash


People also ask

How do I revert my EF core migration?

Reverting a Migration But, for some reason, you want to revert the database to the previous state. In this case, use the update-database <migration name> command to revert the database to the specified previous migration snapshot. > dotnet ef database update MyFirstMigration.

How do I code my first migration to an existing database?

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.

Which command is used to run migration?

There are four available main commands. Enable-Migrations: Enables Code First Migrations in a project. Add-Migration: Scaffolds a migration script for any pending model changes. Update-Database: Applies any pending migrations to the database.


1 Answers

It looks like migration API expects that you want to do downgrade only from "last version".

If BlogLimitsAndTableRename is your most recent migration (the last applied) you can simply run:

Update-Database -Script -TargetMigration:"201205161144187_AddPostAbstract"

If it is not your last migration you need to revert your development database to it first:

Update-Database -TargetMigration:"201205161203310_BlogLimitsAndTableRename"

and now you should be able to use the first command to get a script.

like image 198
Ladislav Mrnka Avatar answered Sep 21 '22 15:09

Ladislav Mrnka