我正在尝试定义以下模型,其中 Appointment 表具有 Person 的外键,并且实体都具有彼此的导航属性。
public class Appointment
{
public int AppointmentId { get; set; }
// Foreign Key property (this will be created in DB)
public int? PersonId { get; set; }
// Navigation property to Flatmate
public virtual Person Person { get; set; }
}
public class Person
{
public int PersonId { get; set; }
// Just navigation property. Don't want Person table to include foreign key (no need)
public virtual Appointment Appointment { get; set; }
}
我尝试使用流利的配置:
modelBuilder.Entity<Appointment>()
.HasOptional(a => a.Person)
.WithOptionalDependent(p=> p.Appointment);
但我得到一个例外,它缺少一列(Appointment_AppointmentId 或 Person_PersonId,取决于我使用的是 WithOptionalDependent 还是 WithOptionalPrincipal)。