Entity Framework 6迁移是否可以包含围绕脚本的事务?
非常简单的问题,我在entity framework6中使用迁移,并且像命令一样
update-database -script
但有没有一种方法可以生成脚本但是用事务包装它?
问题是,如果脚本失败,我必须取消它
这是我在发布模式下仅用于生成脚本的方法:
public class MigrationScriptBuilder : SqlServerMigrationSqlGenerator { #if !DEBUG protected override void Generate(SqlOperation sqlOperation) { Statement("GO"); base.Generate(sqlOperation); Statement("GO"); } public override IEnumerable Generate(IEnumerable migrationOperations, string providerManifestToken) { yield return new MigrationStatement {Sql = "BEGIN TRAN"}; foreach (var migrationStatement in base.Generate(migrationOperations, providerManifestToken)) { yield return migrationStatement; } yield return new MigrationStatement {Sql = "COMMIT TRAN"}; } #endif }
您应该在DatabaseMigrationsConfiguration
。 例:
上述就是C#学习教程:Entity Framework 6迁移是否可以包含围绕脚本的事务?分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
public sealed class DatabaseMigrationsConfiguration : DbMigrationsConfiguration { public DatabaseMigrationsConfiguration() { SetSqlGenerator("System.Data.SqlClient", new MigrationScriptBuilder()); } }
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1034161.html