是否可以仅为模型的一部分生成迁移?

时间:2013-03-11 06:06:36

标签: entity-framework migration ef-migrations

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

我认为在迁移文件中进行更改时第二次调用Add-Migration方法会查看迁移代码并为我应用此迁移时出现的模型制作哈希值。

编辑:

我尝试使用-Force参数调用Add-Migration而不使用-Force参数。当我使用-Force时,如果我在迁移代码中进行了一些额外的更改,那么Up / Down方法的参数迁移代码将会被重写。这就是为什么我认为在这种情况下我不应该使用-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);
    }
  }
}

0 个答案:

没有答案