15

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.

mayash
  • 231
  • 3
  • 9

1 Answers1

16

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.

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • It makes kind of sense. Imagine you have database in state 5 and you'll try to get a script to downgrade from 4 to 3. Then the database can be easily corrupted. Either pointing it to db really in state 4 or having one locally pushed to state 4 makes you think about it before making irreversible mess. OTOH it could be supported with some -Force switch or big red warning. :) – cincura.net Jun 03 '12 at 19:21
  • 2
    I thought about having database in state 5 and now get a script to downgrade it from 5 to 2. EF migration forces me to downgrade from 5 to 4, then to downgrade from 4 to 3 and then from 3 to 2, while all I wish is to produce a single script that will already contain all these downgrades without the need to really execute them. (For upgrade for example you can have a database in state 5 and produce a script of upgrade from 2 to 5). – mayash Jun 04 '12 at 07:20
  • Or you can have a database in state 5 and produce a script of upgrade from 2 to 4. – mayash Jun 04 '12 at 07:32
  • 2
    You can create downgrade script from current version (5) to version 2. You just need to run the first mentioned command with the name of the second migration. – Ladislav Mrnka Jun 04 '12 at 08:20
  • For anybody using the `DbMigrator` and `MigratorScriptingDecorator` via c#, you oddly have to pass `sourceMigration: null` to the migrator's `ScriptUpdate` method. Even if you pass the latest migration id, it still complains that you can't downgrade between two versions (same error as OP) – aaaaaa Oct 18 '17 at 05:37