1

我想使用自定义 VSIX 插件生成迁移,并且只针对模型的一部分。我发现我可以过滤从CSharpMigrationCodeGeneratoroverridnig 方法继承的类中生成的代码Generate(IEnumerable<MigrationOperation> operations, string @namespace, string className)。我在此方法中过滤了一个表的操作。我读到我应该Add-Migration在修改迁移时在包管理器控制台中第二次调用。我做的。然后更新数据库。当我为整个模型创建下一个迁移时,没有生成过滤表的代码,我必须自己为以前过滤的表编写代码。

我认为Add-Migration当我在迁移文件中进行更改时第二次调用方法将查看迁移代码并为模型生成哈希,这将在我应用此迁移时出现。

编辑:

我尝试使用 -Force 参数和不带 -Force 参数来调用 Add-Migration。当我对 Up/Down 方法使用 -Force 参数迁移代码时,如果我对迁移代码进行了一些额外的更改,则将被覆盖。这就是为什么我认为在这种情况下我不应该使用 -Force 参数。

我的 MigrationCodeGenerator 在 Configuration 构造函数中设置正确: CodeGenerator = new MigrationCodeGenerator(tables);

我过滤操作但不生成索引的 MigrationCodeGenerator 代码:

using System.Collections.Generic;
using System.Data.Entity.Migrations.Model;
using System.Data.Entity.Migrations.Utilities;
using System.Data.Entity.Migrations.Design;

namespace MyNamespace.Migrations
{
  public class MigrationCodeGenerator : CSharpMigrationCodeGenerator
  {
    /// <summary>
    /// Tables for which to generate migration file
    /// </summary>
    private List<string> Tables;

    public MigrationCodeGenerator(List<string> tables)
    {
      Tables = tables;
    }

    protected override void GenerateInline(CreateIndexOperation createIndexOperation, IndentedTextWriter writer) { }

    protected override void Generate(CreateIndexOperation createIndexOperation, IndentedTextWriter writer) { }

    protected override void Generate(DropIndexOperation dropIndexOperation, IndentedTextWriter writer) { }

    protected override string Generate(IEnumerable<MigrationOperation> operations, string @namespace, string className)
    {
      if (Tables != null)
      {
        List<MigrationOperation> filteredOperations = new List<MigrationOperation>();

        foreach (var operation in operations)
        {
          if (operation is CreateTableOperation)
          {
            CreateTableOperation createTableOperation = operation as CreateTableOperation;

            if (Tables.Contains(createTableOperation.Name))
              filteredOperations.Add(operation);
          }
          else if (operation is AddColumnOperation)
          {
            AddColumnOperation addColumnOperation = operation as AddColumnOperation;

            if (Tables.Contains(addColumnOperation.Table))
              filteredOperations.Add(operation);
          }
          else if (operation is AddPrimaryKeyOperation)
          {
            AddPrimaryKeyOperation addPrimaryKeyOperation = operation as AddPrimaryKeyOperation;

            if (Tables.Contains(addPrimaryKeyOperation.Table))
              filteredOperations.Add(operation);
          }
          else if (operation is AddForeignKeyOperation)
          {
            AddForeignKeyOperation addForeignKeyOperation = operation as AddForeignKeyOperation;

            if (Tables.Contains(addForeignKeyOperation.DependentTable))
              filteredOperations.Add(operation);
          }
          else if (operation is DropTableOperation)
          {
            DropTableOperation dropTableOperation = operation as DropTableOperation;

            if (Tables.Contains(dropTableOperation.Name))
              filteredOperations.Add(operation);
          }
          else if (operation is DropColumnOperation)
          {
            DropColumnOperation dropColumnOperation = operation as DropColumnOperation;

            if (Tables.Contains(dropColumnOperation.Table))
              filteredOperations.Add(operation);
          }
          else if (operation is DropPrimaryKeyOperation)
          {
            DropPrimaryKeyOperation dropPrimaryKeyOperation = operation as DropPrimaryKeyOperation;

            if (Tables.Contains(dropPrimaryKeyOperation.Table))
              filteredOperations.Add(operation);
          }
          else if (operation is DropForeignKeyOperation)
          {
            DropForeignKeyOperation dropForeignKeyOperation = operation as DropForeignKeyOperation;

            if (Tables.Contains(dropForeignKeyOperation.DependentTable))
              filteredOperations.Add(operation);
          }
          else if (operation is AlterColumnOperation)
          {
            AlterColumnOperation alterColumnOperation = operation as AlterColumnOperation;

            if (Tables.Contains(alterColumnOperation.Table))
              filteredOperations.Add(operation);
          }
          else if (operation is RenameColumnOperation)
          {
            RenameColumnOperation renameColumnOperation = operation as RenameColumnOperation;

            if (Tables.Contains(renameColumnOperation.Table))
              filteredOperations.Add(operation);
          }
          else if (operation is RenameTableOperation)
          {
            RenameTableOperation renameTableOperation = operation as RenameTableOperation;

            if (Tables.Contains(renameTableOperation.Name))
              filteredOperations.Add(operation);
          }
          else if (operation is SqlOperation)
          {
            filteredOperations.Add(operation);
          }
        }

        return base.Generate(filteredOperations, @namespace, className);
      }

      return base.Generate(operations, @namespace, className);
    }
  }
}
4

0 回答 0