MigratorScriptingDecorator

MigratorScriptingDecorator

本文介绍了首先在Entity Framework代码中如何使用MigratorScriptingDecorator?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用

当指定sourceMigration和targetMigration时,我的初始迁移将被写入,其余的迁移将变为空。

When specifying sourceMigration and targetMigration only my initial migration gets written, the rest of my migrations become empty.

我已经输入了包含重现代码的Microsoft Connect错误报告。

I have entered a bug report on Microsoft Connect containing reproducing code.https://connect.microsoft.com/VisualStudio/feedback/details/731111/migratorscriptingdecorator-in-entity-framework-migrations-does-not-respect-sourcemigration-and-targetmigration

我希望 MigratorScriptingDecorator.ScriptUpdate(from,to)的行为完全像相应的PM命令



I expect MigratorScriptingDecorator.ScriptUpdate("from", "to") to behave exactly like the corresponding PM command

PM> Update-Database -Script -SourceMigration from -TargetMigration to

ScriptUpdate应等于更新数据库-Script

有没有其他方法从代码生成更新脚本?

Should ScriptUpdate be equivalent to Update-Database -Script?
Are there any other ways of generating update scripts from code?

推荐答案

如所链接的Microsoft Connect问题所述,问题是在几个 MigratorScriptingDecorator DbMigrator c $ c> s。

As noted on the linked Microsoft Connect issue the problem was a reuse of the same DbMigrator on several MigratorScriptingDecorators.

原始代码是

DbMigrator efMigrator = new DbMigrator(new Configuration());
var pendingMigrations = efMigrator.GetLocalMigrations().ToList();
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); // <-- problem here, the efMigrator is reused several times
    Console.WriteLine("Migration from " + (migration.Item1 ?? "<null> ") + " to " + (migration.Item2 ?? "<null> "));
    Console.WriteLine(sql);
    Console.WriteLine("-------------------------------------");
}

MigratorScriptingDecorator 应该在这个循环外被实例化:

the MigratorScriptingDecorator should be instantiated outside the loop like this:

DbMigrator efMigrator = new DbMigrator(new Configuration());
var pendingMigrations = efMigrator.GetLocalMigrations().ToList();
pendingMigrations.Insert(0, "0");
var scriptMigrator = new MigratorScriptingDecorator(efMigrator); // <-- now only one MigratorScriptingDecorator is created for the DbMigrator
foreach (var migration in pendingMigrations.Zip(pendingMigrations.Skip(1), Tuple.Create))
{
    var sql = scriptMigrator.ScriptUpdate(migration.Item1, migration.Item2);
    Console.WriteLine("Migration from " + (migration.Item1 ?? "<null> ") + " to " + (migration.Item2 ?? "<null> "));
    Console.WriteLine(sql);
    Console.WriteLine("-------------------------------------");
}

这篇关于首先在Entity Framework代码中如何使用MigratorScriptingDecorator?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-08 02:30