EF迁移正在删除列并尝试将不存在的列重命名为Id

时间:2015-07-15 17:36:30

标签: sql entity-framework code-first ef-model-builder

我有一个使用EF的Code First MVC项目。

有两个表,TableATableB,它们目前有一对多的关系:

public partial class TableA
{ 
    public TableA()
    {
        TableBs = new HashSet<TableB>();
    }

    public int Id { get; set; }
    public Virtual ICollection<TableB> TableBs { get; set; }
}

public partial class TableB
{
    public int Id { get; set; }
    public int TableAId { get; set; }
    public virtual TableA TableAs { get; set; }
}

    modelBuilder.Entity<TableA>()
        .HasMany(e => e.TableBs)
        .WithRequired(e => e.TableA)
        .HasForeignKey(e => e.TableAId);

我尝试更改TableA以给它一对一或零关系:

public partial class TableA
{ 
    public TableA() { }

    public int Id { get; set; }
    public int? TableBId { get; set; }
    public virtual TableB TableB { get; set; }
}

TableB保持不变,我改变了我的OnModelCreating

    modelBuilder.Entity<TableA>()
        .HasOptional(e => e.TableBs);

    modelBuilder.Entity<TableB>()
        .HasRequired(e => e.TableAs);

问题在于生成的迁移是意料之外的 - 这对我来说意外,因为我不知道我在做什么,显然。它不是添加FK并添加列,而是尝试重命名列。这是我的迁移:

 public override void Up()
    {
        DropForeignKey("dbo.TableA", "TableBId", "dbo.TableB");
        DropIndex("dbo.TableA", new[] { "TableBId" });
        DropColumn("dbo.TableB", "Id"); 
        RenameColumn(table: "dbo.TableB", name: "TableBId", newName: "Id");
        DropPrimaryKey("dbo.TableB");
        AlterColumn("dbo.TableA", "TableBId", c => c.Int());
        AlterColumn("dbo.TableB", "Id", c => c.Int(nullable: false));
        AlterColumn("dbo.TableB", "TableAId", c => c.Int(nullable: false));
        AddPrimaryKey("dbo.TableB", "Id");
        CreateIndex("dbo.TableB", "Id");
    }

我不明白为什么要重命名一个不存在的列以及我的新FK在哪里?

2 个答案:

答案 0 :(得分:4)

有些事情可能会导致您的问题:

  1. 描述关系的流畅API调用

    modelBuilder.Entity<TableA>()
       .HasOptional(e => e.TableBs);
    
    modelBuilder.Entity<TableB>()
       .HasRequired(e => e.TableAs);
    

    因为你在这里使用两个单独的调用来引用不同的属性,所以我认为EF有可能将其解释为两个独立的关系。另一方面,既然它们都是一对一的,那么它就可以了。无论如何,它会更有意义:

    modelBuilder.Entity<TableA>()
       .HasOptional(e => e.TableBs)
       .WithRequired(t => t.TableAs);
    
  2. 实体之间的任何一对一关系都要求两种实体类型共享主键(其中从属/可选方的主键也是外键),这是主要的生成的迁移是一团糟的原因。

  3. 我认为您可能也曾尝试过去在TableBId中创建了dbo.TableA列的迁移。

答案 1 :(得分:0)

基于jjj所说的我的关系存在严重问题,我希望正常的迁移可以解决它,但jjj是正确的,依赖表的FK和PK必须相同。我正在使用在Azure上运行的SQL服务器,因此我不能只删除列TableB.Id并将TableB.TableAId重命名为TableB.Id,因为它给了我一个聚簇索引错误。他是正确的我的下来声明没有删除列,所以我的TableBId仍然在dbo.TableA。我向我的数据库回滚了大约5次迁移。我对TableA的修订很好:

public partial class TableA
{ 

  public int Id { get; set; }
  public int? TableBId { get; set; }
  public virtual TableB TableB { get; set; }
}

但问题出在TableB我必须从模型中删除TableAId 我在sudo代码中添加了一些列,以使迁移更具可读性:

public partial class TableB
{
  public int Id { get; set; }
  public int Column1 { get; set; }
  public int Column2 { get; set; }
  public int Column3 { get; set; }
  public virtual TableA TableAs { get; set; }
}

现在在我的DbContext中,我选择了一个班轮:

modelBuilder.Entity<TableB>()
            .HasRequired(e => e.TableA).WithOptional(x => x.TableB);

当我添加迁移时,这是新创建的迁移:

     public override void Up()
    {
        DropForeignKey("dbo.TableB", "TableAId", "dbo.TableAId");
        DropIndex("dbo.TableB", new[] { "TableAId" });
        DropColumn("dbo.TableB", "Id");
        RenameColumn(table: "dbo.TableB", name: "TableAId", newName: "Id");
        DropPrimaryKey("dbo.TableB");
        AddColumn("dbo.Tenants", "TableBId", c => c.Int());
        AlterColumn("dbo.TableB", "Id", c => c.Int(nullable: false));
        AlterColumn("dbo.TableB", "Id", c => c.Int(nullable: false));
        AddPrimaryKey("dbo.TableB", "Id");
        CreateIndex("dbo.TableB", "Id");
        AddForeignKey("dbo.TableB", "Id", "dbo.TableA", "Id");
    }

这可能适用于其他版本的SQL但不适用于Azure,因为它不允许堆表,我知道。所以这是我改变迁移代码的方式。

public override void Up()
    {
      <-- I am renaming TableB to TableB_Old just for the migration-->
     RenameTable(name: "dbo.TableB", newName: "TableB_Old");
        DropForeignKey("dbo.TableB", "TableAId", "dbo.TableA");
        DropIndex("dbo.TableB_Old", new[] { "TableAId" });
        AlterColumn("dbo.TableB_Old", "TableAId", c => c.Int(nullable: false));
<-- Create a new TableB with the correct layout notice TableAId is not in the column list, so this still matches up to my code model-->
        CreateTable(
           "dbo.TableB",
           c => new
           {
               Id = c.Int(nullable: false)
               Column1 = c.Int(nullable: false)
               Column2 = c.Int(nullable: false)
               Column3 = c.Int(nullable: false)
           })
           .PrimaryKey(t => t.Id)
           .ForeignKey("dbo.TableA", t => t.Id)
           .Index(t => t.Id);

        AddColumn("dbo.TableA", "TableBId", c => c.Int());
<-- This pulls the data from tableB_Old into the newly created TableB, since    TableA had a one-to-many relationship I had to get rid of duplicates this gets  the first record with TableAId and discards the rest. -->

Sql("INSERT INTO [dbo].[TableB] (Id,Column1,Column2,Column3) SELECT     TableAId,,Column1,Column2,Column3 FROM [dbo].[TableB_Old] where [Id] In (select min([Id]) from[dbo].[TableB_Old] as T2 where T2.TableAId = [dbo].[TableB_Old].TableAId)");

<--Now that we pulled all column data we needed from `TableB_Old` we can populate `TableA.TableBId`-->

Sql("UPDATE [TableA] SET [TableBId] = (SELECT [Id] FROM [TableB] WHERE [TableB].[Id] = [TableA].[Id])");

<-- `TableB_Old` was a good table, but it has out lived it's usefullness, it must be removed -->


        DropTable("dbo.TableB_Old");