0
我想要定義以下模型,其中約會表具有用於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)。