一对多关系,配置在实体框架中引用哪个字段

本文关键字:引用 字段 框架 实体 关系 配置 一对多 | 更新日期: 2023-09-27 18:04:38

域名如下:

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; }
}

下面是我的配置:

HasRequired(p => p.Person).WithMany().HasForeignKey(pc => pc.PersonId);

我的问题是如何配置Person的Children属性以引用PersonChild中的personid ?

一对多关系,配置在实体框架中引用哪个字段

我的问题的解决方案是指定配置中引用的字段:

HasRequired(pc => pc.Person).WithMany(p => p.Children).HasForeignKey(pc => pc.PersonId);