Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

235
Views
Entity Framework 6.2 migration tooling only supports scripting a downward migration when connected to an actual database

Attempting to script migrations from Entity Framework 6.2 code, but downward migration script generation is failing when both the source and target migrations are explicitly specified.

For example, I have a test DbContext project that contains three migrations:

  1. 201506161504528_InitialCreate
  2. 202202102238227_Add_Entity_FantasticalCreature
  3. 202202102239481_FantasticalCreature_Add_IsWinged_Add_HasTail

Scripting an upward migration explicitly works fine with these commands:

Update-Database -SourceMigration "201506161504528_InitialCreate" `
      -TargetMigration "202202102238227_Add_Entity_FantasticalCreature" `
      -Script -Verbose


.\migrate.exe "TestContext.dll" 
       /targetMigration="202202102238227_Add_Entity_FantasticalCreature" 
       /scriptFile="script.sql" 
       /sourceMigration="201506161504528_InitialCreate" /verbose

Furthermore, using MigratorScriptingDecorator and the ScriptUpdate with similar parameters will also generate the upward script when explicitly specifying both source and target migrations.

However, all of the methods described above will fail when attempting to do the opposite, i.e. script a downward migration in which both the target and source are both explicitly specified and where source is a "lower" MigrationId.

For example, the Update-Database invoked like this:

update-database -SourceMigration "202202102238227_Add_Entity_FantasticalCreature" `
                -TargetMigration "201506161504528_InitialCreate" `
                -Script -Verbose

results in an exception:

System.Data.Entity.Migrations.Infrastructure.MigrationsException: Scripting the downgrade between two specified migrations is not supported.

at System.Data.Entity.Migrations.Infrastructure.MigratorScriptingDecorator.ScriptUpdate(String sourceMigration, String targetMigration)
at System.Data.Entity.Migrations.Design.ToolingFacade.ScriptUpdateRunner.RunCore()
at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.Run()

Scripting the downgrade between two specified migrations is not supported.

The same exception occurs attempting to do the explicit downward script migration using either migrate.exe or MigratorScriptingDecorator.ScriptUpdate as well.

However, if the source migration is not specified explicitly, a target migration is specified, and a connection string is provided to a database that is at a "higher" migration than the specified target migration, then all three methods described above will successfully generate a downward migration SQL script.

For example, successfully generating the downward migration using Update-Database:

# Assumes TestContextDb-01 is currently at migration 2 or 3.
Update-Database -TargetMigration "201506161504528_InitialCreate" `
                -Script `
                -ConnectionString "Data Source=localhost; Initial Catalog=TestContextDb-01; Integrated Security=true;" `
                -ConnectionProviderName "System.Data.SqlClient" -Verbose

Why is it that:

  • Upward migration script generation works using explicit source and target migration, but the same is not supported for downward migration?
  • Downward migration script generation only works when specifying an actual database to connect to?
  • Is there any way to generate a downward migration without having to specify a database to connect to?
  • Why the inconsistency? Wouldn't downward script migration just be the inverse of upward?

FWIW, I have a suspicion that a localdb or SQL Server Express database may be implicitly created by the migration tooling in order to do the explicit upward script migration. I don't know how that necessarily plays into everything laid out above, nor do I understand why the same could not still be done for explicit downward script migration.

Any insight is greatly appreciated!

over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!