Moving EF removes the column and attempts to rename the nonexistent column back to Id

I have a first MVC project using EF.

There are two tables TableAand TableB, and they currently have a one-to-many relationship:

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);

I tried changing TableAto give it a one-to-zero relationship:

public partial class TableA
{ 
    public TableA() { }

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

TableBremains unchanged, and I change my OnModelCreatingas follows:

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

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

The problem is that the generated migration is unexpected - unexpected for me, because I don't know what I'm doing, obviously. Instead of adding FK and adding columns, it tries to rename the columns. Here is my migration:

 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");
    }

I don’t understand why he is trying to rename a nonexistent column and where are my new FKs?

+4
2

:

  • 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);
    
  • , ( / ), .

  • , , , TableBId dbo.TableA.

+3

, jjj , , , , jjj , FK PK . SQL-, Azure, dropcolumn 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 usefullness, it must be removed -->


        DropTable("dbo.TableB_Old");
0

Source: https://habr.com/ru/post/1598089/


All Articles