C# 我可以在EF代码优先模型中拥有相关的导航属性吗

C# 我可以在EF代码优先模型中拥有相关的导航属性吗,c#,.net,ef-code-first,entity-framework-5,dbcontext,C#,.net,Ef Code First,Entity Framework 5,Dbcontext,假设我有以下模型 public class Human { public int HumanId {get;set;} public int Gender {get;set;} // 0 = male, 1 = female public int? FatherId {get;set;} [ForeignKey("FatherId")] public virtual Human Father {get;set;} public int? Mot

假设我有以下模型

public class Human
{
    public int HumanId {get;set;}
    public int Gender {get;set;} // 0 = male, 1 = female

    public int? FatherId {get;set;}
    [ForeignKey("FatherId")]
    public virtual Human Father {get;set;}

    public int? MotherId {get;set;}
    [ForeignKey("MotherId")]
    public virtual Human Mother {get;set;}

    public virtual List<Human> Children {get;set;}        
}
公共类人员
{
public int HumanId{get;set;}
公共int性别{get;set;}//0=男性,1=女性
公共int?父ID{get;set;}
[外籍人士(“父亲”)]
公共虚拟人父{get;set;}
公共int?MotherId{get;set;}
[ForeignKey(“MotherId”)]
公共虚拟人母亲{get;set;}
公共虚拟列表子项{get;set;}
}
好的,这是一种自参考方法。对于父/母映射,我通过在DbContext类中编写此代码找到了解决方案

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {

        modelBuilder.Entity<Human>()
            .HasOptional(h => h.Father)
            .WithMany()
            .Map(h => h.MapKey("Father"));

        modelBuilder.Entity<Human>()
            .HasOptional(h => h.Mother)
            .WithMany()
            .Map(h => h.MapKey("Mother"));

    }
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
modelBuilder.Entity()
.has可选(h=>h.Father)
.有很多
.Map(h=>h.MapKey(“父”);
modelBuilder.Entity()
.has可选(h=>h.Mother)
.有很多
.Map(h=>h.MapKey(“母亲”);
}
但我对儿童财产感到困惑,因为它取决于一个条件(性别) 通常我会写这样的东西

public virtual List<Human> Children
{
    get
    {
        if (this.Gender == 0)
            return Context.Humans.Where(x => x.FatherId == this.Id).ToList();
        else if (this.Gender == 1)
            return Context.Humans.Where(x => x.MotherId == this.Id).ToList();
        else
            return null;
    }
}
公共虚拟列表子项
{
收到
{
如果(this.Gender==0)
返回Context.Humans.Where(x=>x.FatherId==this.Id).ToList();
else if(this.Gender==1)
返回Context.Humans.Where(x=>x.MotherId==this.Id).ToList();
其他的
返回null;
}
}
但在我的模型课上,我不知道上下文

那么,解决这个问题的最佳方法是什么?目前我有一个方法

public List<Human> GetChildren(Human human) { ... }
public List GetChildren(人类){…}

在我的DbContext类中,但我更喜欢在我的模型中使用它。是否可以?

再创建一个外键作为常规父项。我还没有测试过它,但理论上它应该是有效的

public class Human
{
    public int HumanId {get;set;}
    public int Gender {get;set;} // 0 = male, 1 = female

    public int ParentId { get; private set; }
    public virtual Human Parent { get; private set;}

    public int FatherId {get; private set;}
    public virtual Human Father {get; private set;}

    public int MotherId {get; private set;}
    public virtual Human Mother {get; private set;}

    public virtual List<Human> Children {get;set;}        

    public void SetParent(Human parent)
    {
        Parent = parent;
        if (parent.Gender ==0)
          Father = parent;
        else
          Mother = parent;
    }
}
公共类人员
{
public int HumanId{get;set;}
公共int性别{get;set;}//0=男性,1=女性
public int ParentId{get;private set;}
公共虚拟人父项{get;private set;}
public int FatherId{get;private set;}
公共虚拟人父{get;私有集;}
public int MotherId{get;private set;}
公共虚拟人母亲{get;私有集;}
公共虚拟列表子项{get;set;}
public void SetParent(人父)
{
父母=父母;
如果(parent.Gender==0)
父亲=父母;
其他的
母亲=父母;
}
}
DbContext

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{

    modelBuilder.Entity<Human>()
        .HasOptional(h => h.Father)
        .WithMany()
        .Map(h => h.MapKey("Father"));

    modelBuilder.Entity<Human>()
        .HasOptional(h => h.Mother)
        .WithMany()
        .Map(h => h.MapKey("Mother"));

    modelBuilder.Entity<Human>()
        .HasOptional(h => h.Parent)
        .WithMany(x => x.Children)
        .Map(h => h.MapKey("Parent"));

}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
modelBuilder.Entity()
.has可选(h=>h.Father)
.有很多
.Map(h=>h.MapKey(“父”);
modelBuilder.Entity()
.has可选(h=>h.Mother)
.有很多
.Map(h=>h.MapKey(“母亲”);
modelBuilder.Entity()
.has可选(h=>h.Parent)
.有许多(x=>x个孩子)
.Map(h=>h.MapKey(“父”);
}

我不知道这是否是我需要的。一个人可以有两个父母(父亲和母亲)