实体框架迁移:仅在-Script输出中包含Go语句 [英] Entity Framework Migrations: Including Go statement only in -Script output

查看:110
本文介绍了实体框架迁移:仅在-Script输出中包含Go语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为计划实体框架迁移的一部分,为了调试数据移动,我经常会使用-Script参数生成脚本。

As part of planning an Entity Framework migration, in order to debug data movement, I would often use the -Script parameter to generate the script.

我可以将此脚本转到查询分析器并将其包装在事务中,以便手动测试。

I could then take this script to Query Analyzer and wrap it in a transaction in order to test it manually.

我遇到一个需要一个Go语句来正确执行脚本的情况。将以下代码添加到迁移中,以便在正确的位置输出Go。

I came across a situation where we needed a Go statement to execute the script properly. The following code was added to the migration in order to output a Go in the proper place.

Sql("GO");

当使用-Script时,会在正确的位置添加一个GO语句。但是当不使用-Script时。我得到例外...

This adds a GO statement in the proper position when -Script is used. But when -Script isn't used. I get the exception...

System.Data.SqlClient.SqlException (0x80131904): Could not find stored procedure 'GO'.

有没有一个安全的方法来添加一个Go命令到脚本?

Is there a safe way to add a Go command to the script?

推荐答案

internal sealed class Configuration : DbMigrationsConfiguration<Context>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = false;
        const string providerInvariantName = "System.Data.SqlClient";
        SetSqlGenerator(providerInvariantName, new BatchingMigrationSqlGenerator(GetSqlGenerator(providerInvariantName)));
    }

    protected override void Seed(Context context)
    {
    }

}

internal class BatchingMigrationSqlGenerator : MigrationSqlGenerator
{
    private readonly MigrationSqlGenerator migrationSqlGenerator;

    public BatchingMigrationSqlGenerator(MigrationSqlGenerator migrationSqlGenerator)
    {
        this.migrationSqlGenerator = migrationSqlGenerator;
    }

    public override IEnumerable<MigrationStatement> Generate(IEnumerable<MigrationOperation> migrationOperations, string providerManifestToken)
    {
        var migrationStatements = migrationSqlGenerator.Generate(migrationOperations, providerManifestToken).ToArray();
        foreach (var migrationStatement in migrationStatements)
        {
            migrationStatement.BatchTerminator = "GO";
        }
        return migrationStatements;
    }
}

这篇关于实体框架迁移:仅在-Script输出中包含Go语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆