gpt4 book ai didi

c# - Entity Framework 代码优先 - 将两个字段联合到一个集合中

转载 作者:太空狗 更新时间:2023-10-29 20:55:13 26 4
gpt4 key购买 nike

我有这个型号和配置

public class Person
{
public int? FatherId { get; set; }
public virtual Person Father { get; set; }
public int? MotherId { get; set; }
public virtual Person Mother { get; set; }
public virtual List<Person> Childs { get; set; }

}
class PersonConfiguration : EntityTypeConfiguration<Person>
{
public PersonConfiguration()
{
HasOptional(e => e.Father).WithMany(e => e.Childs)
.HasForeignKey(e => e.FatherId);
HasOptional(e => e.Mother).WithMany(e => e.Childs)
.HasForeignKey(e => e.MotherId);
}
}

我在类型为初始的地方收到此错误。

Schema specified is not valid. Errors: (151,6) : error 0040: Type Person_Father is not defined in namespace ExamModel (Alias=Self).

有没有办法通过两个属性(motherId 和 fatherId)映射 Childs 属性?

最佳答案

不可能将两个导航属性映射到一个集合属性。看起来很调侃但是你得有两个集合属性

public class Person
{
public int? FatherId { get; set; }
public virtual Person Father { get; set; }
public int? MotherId { get; set; }
public virtual Person Mother { get; set; }
public virtual List<Person> ChildrenAsFather { get; set; }
public virtual List<Person> ChildrenAsMother { get; set; }
}

class PersonConfiguration : EntityTypeConfiguration<Person>
{
public PersonConfiguration()
{
HasOptional(e => e.Father).WithMany(e => e.ChildrenAsFather)
.HasForeignKey(e => e.FatherId);
HasOptional(e => e.Mother).WithMany(e => e.ChildrenAsMother)
.HasForeignKey(e => e.MotherId);
}
}

关于c# - Entity Framework 代码优先 - 将两个字段联合到一个集合中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9081256/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com