C# 一对多关系,配置实体框架中引用的字段
以下是域:C# 一对多关系,配置实体框架中引用的字段,c#,entity-framework,C#,Entity Framework,以下是域: public class Person : Entity { public int Id { get; set; } pucli ICollection<PersonChild> Children } public class PersonChild { public int Id { get; set; } public int PersonId { get; set; } public Person Person { get;
public class Person : Entity
{
public int Id { get; set; }
pucli ICollection<PersonChild> Children
}
public class PersonChild
{
public int Id { get; set; }
public int PersonId { get; set; }
public Person Person { get; set; }
public int ChildId { get; set; }
public Person Child { get; set; }
}
我的问题是如何配置Person的Children属性以引用PersonChild中的PersonId?我的当前配置继续引用ChildID。我的问题的解决方案是指定在配置中引用哪个字段:
HasRequired(p => p.Person).WithMany().HasForeignKey(pc => pc.PersonId);
HasRequired(pc => pc.Person).WithMany(p => p.Children).HasForeignKey(pc => pc.PersonId);