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:
201506161504528_InitialCreate202202102238227_Add_Entity_FantasticalCreature202202102239481_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!