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?
A couple things might be causing your issues:
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);
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.
I think it's also possible you've tried to run a migration in the past that created the column TableBId in dbo.TableA.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With