当我从Nuget Package Manager控制台运行update-database -Script
时,Visual Studio会生成一个SQL脚本,我可以运行该脚本来更新数据库。
有没有办法在C#中生成这个SQL脚本而不运行它?
这样我就可以在更新数据库之前向正在更新应用程序的用户显示它。
答案 0 :(得分:2)
您可以使用DbMigrator和MigratorScriptingDecorator:
var configuration = new DbMigrationsConfiguration
{
ContextType = typeof(ApplicationDbContext),
MigrationsAssembly = typeof(ApplicationDbContext).Assembly,
MigrationsNamespace = "YourNamespace.Migrations",
AutomaticMigrationsEnabled = true
};
var dbMigrator = new DbMigrator(configuration);
var migratorScriptingDecorator = new MigratorScriptingDecorator(dbMigrator);
string script = migratorScriptingDecorator.ScriptUpdate(sourceMigration: null, targetMigration: null);