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?