Home Dashboard Directory Help

MigratorScriptingDecorator in Entity Framework Migrations does not respect sourceMigration and targetMigration by Albin Sunnanbo


 as Deferred Help for as Deferred

Sign in
to vote
Type: Bug
ID: 731111
Opened: 3/14/2012 4:54:36 AM
Access Restriction: Public
Moderator Decision: Sent to Engineering Team for consideration
User(s) can reproduce this bug


I have tried to use MigratorScriptingDecorator to generate SQL Update scripts

            DbMigrator efMigrator = new DbMigrator(new Configuration());
            var pendingMigrations = efMigrator.GetLocalMigrations().ToList();
            if (false) // try both true and false.
                pendingMigrations.Insert(0, null);
                pendingMigrations.Insert(0, "0");
            foreach (var migration in pendingMigrations.Zip(pendingMigrations.Skip(1), Tuple.Create))
                var sql = new MigratorScriptingDecorator(efMigrator).ScriptUpdate(migration.Item1, migration.Item2);
                Console.WriteLine("Migration from " + (migration.Item1 ?? "<null> ") + " to " + (migration.Item2 ?? "<null> "));
I expect the .ScriptUpdate(source, target); to behave like the package-manager console
The first part
PM> Update-Database -Script -SourceMigration "sourcename" -TargetMigration "targetname"

but I don't get migration code for all my migrations
Sign in to post a comment.
Posted by Albin Sunnanbo on 3/19/2012 at 4:36 AM
I got my main case working too.
I had another problem there
I had written

    DbMigrator efMigrator = new DbMigrator(new DbMigrationsConfiguration<MyDbContext>());

All my sql-scripts became empty, but when I changed that to

    DbMigrator efMigrator = new DbMigrator(new Configuration());

it worked OK.

Should I somehow close this issue?
Posted by Albin Sunnanbo on 3/16/2012 at 12:09 PM
Tanks again for investigating.
My demo case seems to run fine by now, I'll verify the solution on my big real case at work on monday.

// Albin
Posted by Microsoft on 3/16/2012 at 10:58 AM

The issue you are hitting is related to the decorator pattern we have used for the scripting migrator. The issue is that you are using the same DbMigrator but creating a new MigratorScriptingDecorator inside each iteration of the for loop. The result is a DbMigrator wrapped in multiple MigratorScriptingDecorator.

We've seen a few people hit this issue and realize it's easy to hit because the API surface is confusing. We have added a backlog item to add a simpler API surface to script migrations.

In the mean time you can just move the scripter outside the for loop and your code works as expected:

var scripter = new MigratorScriptingDecorator(efMigrator);
     foreach (var migration in pendingMigrations.Zip(pendingMigrations.Skip(1), Tuple.Create))
        var sql = scripter.ScriptUpdate(migration.Item1, migration.Item2);

~Entity Framework Team
Posted by Albin Sunnanbo on 3/14/2012 at 11:40 PM
Thanks for investigating the issue.
To give a little background we are trying to build an automatic script generator that will be run by our continuous integration system.
Posted by MS-Moderator07 [Feedback Moderator] on 3/14/2012 at 9:40 PM
Thanks for your feedback.

We are rerouting this issue to the appropriate group within the Visual Studio Product Team for triage and resolution. These specialized experts will follow-up with your issue.
Posted by MS-Moderator07 [Feedback Moderator] on 3/14/2012 at 9:40 PM
Posted by MS-Moderator01 on 3/14/2012 at 5:53 AM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(http://support.microsoft.com)
Posted by Albin Sunnanbo on 3/14/2012 at 4:59 AM
Added complete solution that reproduces the problem.
Sign in to post a workaround.
File Name Submitted By Submitted On File Size  
MigrationsDemo.zip 3/14/2012 1.16 MB