I am doing code-first entity framework design.
I have a table, Account, which has a property, Supervisor:
public class Account
{
public int Id { get; set; }
public Account Supervisor { get; set; }
}
This works beautifully.
However, I wish to add an alternate supervisor to the class:
public class Account
{
public int Id { get; set; }
public Account Supervisor { get; set; }
public Account AlternateSupervisor { get; set; }
}
When I run Add-Migration AddAlternateSupervisor, the generated code gives me the following:
public partial class AddAlternateSupervisor : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_Accounts_Accounts_SupervisorId",
table: "Accounts");
migrationBuilder.DropIndex(
name: "IX_Accounts_SupervisorId",
table: "Accounts");
migrationBuilder.AddColumn<int>(
name: "AlternateSupervisorId",
table: "Accounts",
nullable: true);
migrationBuilder.CreateIndex(
name: "IX_Accounts_AlternateSupervisorId",
table: "Accounts",
column: "AlternateSupervisorId",
unique: true,
filter: "[AlternateSupervisorId] IS NOT NULL");
migrationBuilder.AddForeignKey(
name: "FK_Accounts_Accounts_AlternateSupervisorId",
table: "Accounts",
column: "AlternateSupervisorId",
principalTable: "Accounts",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
}
// snip
}
As you can see, EF is trying to rename my reference from Supervisor to AlternateSupervisor. I don't want that, I want both Supervisor and AlternateSupervisor to reference other accounts.
I know that EF can't handle multiple many-to-many relationships, but these are one to one relationships. I can't seem to find any information on why EF is generating the migration like this.
So why is Entity Framework trying to rename Supervisor to AlternateSupervisor and how can I force it to generate both links?
EDIT: This question was answered as initially asked. However, I would like to add that as asked the question doesn't really make much domain sense. Who ever heard of an account that could only ever supervise exactly one other account? The relationship is a one to many relationship, which is resolved by the use of WithMany instead of WithOne.