这是问题的完美例证。如您所见,我无法合并主键。而且我不想模拟1 .. *关系,因为这种关系总是1-1。
public class SingleParent
{
public Guid ParentID { get; set; }
public virtual OnlyChild Child { get; set; }
}
public class OnlyChild
{
public Guid ChildID { get; set; }
public virtual SingleParent Parent { get; set; }
}
// Parent and Child cannot share a primary key because a pediatrician only treats children.
public class Pediatrician
{
public Guid PediatricianID { get; set; }
public virtual List<OnlyChild> Patients;
}
如何获取导航属性,使Parent.Child&lt; ==&gt; Child.Parent?我更愿意使用数据注释与其余代码保持一致。任何智慧都将不胜感激!
答案 0 :(得分:0)
如果您向两个实体添加外键,它应该同时使用DataAnnotations和Fluent API。
public class SingleParent
{
public Guid ParentID { get; set; }
public Guid ChilId { get; set; }
[ForeignKey("ChildId")]
public virtual OnlyChild Child { get; set; }
}
public class OnlyChild
{
public Guid ChildID { get; set; }
public Guid ParentId { get; set; }
[ForeignKey("ParentId")]
public virtual SingleParent Parent { get; set; }
}
Here is some documentation解释了如何创建一对一的关系。