我有这个型号和配置
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).
解决方法
无法将两个导航属性映射到单个集合属性.它看起来很嘲笑,但你必须有两个集合属性
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); } }