将“GO”语句添加到实体框架迁移 [英] Adding 'GO' statements to Entity Framework migrations

查看:97
本文介绍了将“GO”语句添加到实体框架迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个由Entity框架进行大量迁移的应用程序。
我们想要一次获得所有迁移的脚本,并使用 -Script 标记可以正常工作。



但是...它不会在SQL中添加 GO 语句,给我们的问题像更改视图应该是批处理中的第一个语句文件...



我一直在搜索,手动添加 Sql(GO); 帮助这个问题,但整个脚本仅 。当我再次使用包控制台管理器时,它返回一个异常。

  System.Data.SqlClient.SqlException(0x80131904):无法找到存储过程'GO'。 

有没有办法添加这些 GO 使用 -Script 标签时,仅标记?
如果没有,这是一个很好的方法?



注意:我们也尝试了多个文件,但是由于我们有这么多的迁移,

解决方案

为了更改由实体框架迁移生成的SQL,您可以创建一个新的 SqlServerMigrationSqlGenerator



我们已经做到这一点,在迁移历史记录之前和之后添加一个GO语句:

  public class MigrationScriptBuilder:SqlServerMigrationSqlGenerator 
{
protected override void Generate(System.Data.Entity.Migrations.Model.InsertHistoryOperation insertHistoryOperation)
{
语句(GO);

base.Generate(insertHistoryOperation);

声明(GO);

}
}

然后添加配置构造函数(在DbContext所在项目的 Migrations 文件夹中),以便使用此新的sql生成器:


  [...] 
内部密封类配置:DbMigrationsConfiguration< PMA.Dal.PmaContext>
{
public Configuration()
{
SetSqlGenerator(System.Data.SqlClient,新的MigrationScriptBuilder());
AutomaticMigrationsEnabled = false;
}
[...]

所以现在当你生成一个脚本使用-Script标签,您可以看到<_ c $ c>插入到[__MigrationHistory] ​​中的 GO



或者在您执行 SqlServerMigrationSqlGenerator 时,您可以覆盖脚本生成的任何部分, InsertHistoryOperation 适合我们。


So I have an application with a ton of migrations made by Entity framework. We want to get a script for all the migrations at once and using the -Script tag does work fine.

However...it does not add GO statements in the SQL giving us problems like Alter view should be the first statement in a batch file...

I have been searching around and manually adding Sql("GO"); help with this problem but only for the entire script. When I use the package console manager again it returns an exception.

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

Is there a way to add these GO tags only when using the -Script tag? If not, what is a good approach for this?

Note: we have also tried having multiple files but since we have so many migrations, this is near impossible to maintain every time.

解决方案

In order to change the SQL Generated by entity framework migrations you can create a new SqlServerMigrationSqlGenerator

We have done this to add a GO statement before and after the migration history:

public  class MigrationScriptBuilder: SqlServerMigrationSqlGenerator
{
    protected override void Generate(System.Data.Entity.Migrations.Model.InsertHistoryOperation insertHistoryOperation)
    {
        Statement("GO");

        base.Generate(insertHistoryOperation);

        Statement("GO");

    }
}

then add in the Configuration constructor (in the Migrations folder of the project where you DbContext is) so that it uses this new sql generator:

[...]
internal sealed class Configuration : DbMigrationsConfiguration<PMA.Dal.PmaContext>
{
    public Configuration()
    {
        SetSqlGenerator("System.Data.SqlClient", new MigrationScriptBuilder());
        AutomaticMigrationsEnabled = false;
    }
[...]

So now when you generate a script using the -Script tag, you can see that the insert into [__MigrationHistory] is surrounded by GO

Alternatively in your implementation of SqlServerMigrationSqlGenerator you can override any part of the script generation, the InsertHistoryOperation was suitable for us.

这篇关于将“GO”语句添加到实体框架迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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