我有以下两个类,我想使用Code First approch在这两个实体之间建立一对一的关系。任何人都可以建议/帮助我如何在这些患者和他人之间建立一对一的关系。地址实体?提前谢谢......
public partial class Patient
{
public Patient()
{
this.Clinicals = new List<Clinical>();
}
public int PatientId { get; set; }
public string MaritalStatus { get; set; }
public Nullable<System.DateTime> ModifiedDate { get; set; }
public virtual ICollection<Clinical> Clinicals { get; set; }
[Required]
public virtual Address Address { get; set; }
}
public partial class Address
{
public int AddressId { get; set; }
[ForeignKey("Patient")]
public int PatientId { get; set; }
public string AddressLine1 { get; set; }
public string State { get; set; }
public Nullable<System.DateTime> ModifiedDate { get; set; }
public virtual Patient Patient { get; set; }
}
答案 0 :(得分:0)
如果您运行应用程序,Entity Framework将自动为您创建one-to-one
关系。因为您已为此添加了必要的属性。无论如何,如果你想手动创建它,你可以使用Fluent API:
modelBuilder.Entity<Patient>()
.HasRequired(a => a.Address)
.WithRequiredPrincipal(p => p.Patient);