one-to-one
relationship with explicit FK property (like your PayGroup.SupervisorId
) is not supported.
So remove that property from the model:
public class PayGroup
{
public int Id { get; set; }
public virtual Employee Supervisor { get; set; }
}
and use the following fluent mapping:
modelBuilder.Entity<PayGroup>()
.HasRequired(e => e.Supervisor)
.WithOptional()
.Map(m => m.MapKey("SupervisorId"));
The WithOptional()
call specifies two things. First that there is no inverse navigation property in Employee
class, and second that the FK is optional (Allow Nulls = true
in the table).
If you decide to add inverse navigation property
public class Employee
{
public string EmployeeId { get; set; }
public string FullName { get; set; }
public virtual PayGroup PayGroup { get; set; } // <=
}
change it to WithOptional(e => e.PayGroup)
.
If you want to make it required (Allow Nulls = false
in the table), then use the corresponding WithRequiredDependent
overload (Dependent here means that the Employee
will be the principal and PayGroup
will be the dependent).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…