Add-Migration在VS2017和VS2015中生成不同的迁移

时间:2019-03-14 18:01:34

标签: entity-framework visual-studio-2015 visual-studio-2017 entity-framework-6

我们最近将EF Code First应用程序从VS2015迁移到VS2017。团队中的一些开发人员(但不是全部)在VS2015和VS2017上调用Add-Migration会得到不同的结果。

例如,如果我在VS2015上生成了一个迁移,而没有进行重大的模型更改并且没有重新生成,我将获得预期的空迁移

Add-Migration ... -Name ShouldBeAnEmptyMigration

给我:

namespace Blah.Foo.DataModel.Migrations
{
    using System;
    using System.Data.Entity.Migrations;

    public partial class ShouldBeAnEmptyMigration: DbMigration
    {
        public override void Up()
        {
        }

        public override void Down()
        {
        }
    }
}

如果我回滚这些更改并在VS2017中针对相同的数据库运行相同的命令,则会得到充满不想要的更改的迁移:

namespace Blah.Foo.DataModel.Migrations
{
    using System;
    using System.Data.Entity.Migrations;

    public partial class ShouldBeAnEmptyMigration: DbMigration
    {
        public override void Up()
        {
            // For some reason EF is trying to add foreign key columns that
            // already exist except with an additional suffix of "1".
            // e.g. We already have an FK from 'Child' to 'Parent' using
            // Guid Parent_Id which has been in place for (literally) years.
            // There are 11 such columns it is trying to add here.
            // Some date base a couple years, some are more recent.
            // I don't see a pattern for which columns it is choosing

            // 11 of these, one for each "new" column ...
            DropForeignKey("Child", "Parent_Id", "Parent");
            ...
            // 11 of these ...
            DropIndex("Child", new[] { "Parent_Id" });
            ...
            // 11 of these ...
            AddColumn("Child", "Parent_Id1", c => c.Guid(nullable: false));
            ...
            // 11 of these ...
            CreateIndex("Child", "Parent_Id1");
            ...
            // 11 of these ...
            AddForeignKey("Child", "Parent_Id1", "Parent", "Id");
            ...
        }

        public override void Down()
        {
            // similar set of DropForeignKey, DropIndex, DropColumn,
            // CreateIndex, AddForeignKey
        }
    }
}

请注意,大多数受影响的DataModel都没有最新更改。

当前,我被迫在VS2015上创建所有迁移。有什么方法可以使VS2017“表现”?

版本:

  • Visual Studio Pro 2015(14.0.25431.01更新3)
  • Visual Studio Pro 2017(15.9.7)
  • EntityFramework.6.2.0

0 个答案:

没有答案