Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF migration is dropping a column and trying to rename a nonexistent column back to Id

I have a Code First MVC Project using EF.

There are two tables, TableA and 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 to change TableA to give it a one-to-zero-or-one relationship:

public partial class TableA
{ 
    public TableA() { }

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

TableB stays the same, and I change my OnModelCreating like so:

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

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

The problem is that the generated migration is unexpected -- unexpected to me since I don't know what I'm doing, obviously. Instead of adding the FK and adding the columns it is trying to rename 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 is it trying to rename a nonexistent column and where are my new FKs?

like image 326
Landy Avatar asked Sep 28 '22 08:09

Landy


1 Answers

A couple things might be causing your issues:

  1. The fluent API calls describing the relationship

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

    Because you used two separate calls here that reference different properties, I'm thinking that there's a possibility EF is interpreting this as two separate relationships. On the other hand, since they're both one-to-one, it miiiight be okay. In any case, it would make more sense as:

    modelBuilder.Entity<TableA>()
       .HasOptional(e => e.TableBs)
       .WithRequired(t => t.TableAs);
    
  2. Any kind of one-to-one relationship between entities require both entity types to share a primary key (where the dependent/optional side's primary key is also the foreign key), which is the main reason the generated migration is a mess.

  3. I think it's also possible you've tried to run a migration in the past that created the column TableBId in dbo.TableA.

like image 150
jjj Avatar answered Oct 17 '22 01:10

jjj