简体   繁体   English

实体框架代码优先 - 将两个字段联合成一个集合

[英]entity framework code first - Union of the two fields into one collection

i have this model and configuration 我有这个型号和配置

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

and i get this error where the type is initial. 我得到这个类型是初始的错误。

Schema specified is not valid. 指定的架构无效。 Errors: (151,6) : error 0040: Type Person_Father is not defined in namespace ExamModel (Alias=Self). 错误:(151,6):错误0040:在名称空间ExamModel(Alias = Self)中未定义类型Person_Father。

Is there a way to map Childs property by both properties (motherId and fatherId)? 有没有办法通过两个属性(motherId和fatherId)映射Childs属性?

Its not possible to map two navigational properties to a single collection property. 无法将两个导航属性映射到单个集合属性。 It looks ridicules but you have to have two collection properties 它看起来很嘲笑,但你必须有两个集合属性

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

Thank you, Eranga, your reply is exactly what I needed! 谢谢你,Eranga,你的回复正是我所需要的!

Additionally, here is the modelBuilder code if anyone is using that method instead of the configuration method that Eranga used. 另外,如果有人使用该方法而不是Eranga使用的配置方法,这里是modelBuilder代码。

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Person>().
    HasKey(i => i.PersonId);

    modelBuilder.Entity<Person>().
    HasOptional(f => f.Father).
    WithMany(f => f.ChildrenAsFather).
    HasForeignKey(f => f.FatherId);

    modelBuilder.Entity<Person>().
    HasOptional(m => m.Mother).
    WithMany(m => m.ChildrenAsMother).
    HasForeignKey(m => m.MotherId);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 实体框架代码前两个导航属性到/来自一个抽象实体 - Entity Framework Code First Two navigationProperty to/from one abstract entity 实体框架代码优先:两个DbContext和一个数据库初始化程序 - Entity Framework Code First : Two DbContext and one database initializers 条件两个属性之一必须为null Entity Framework Code First - Condition one of two properties to be different of null Entity Framework Code First 首先在实体框架代码中连接具有一对多关系的两个表 - Joining two tables with one to many relatipnship in entity framework code first Entity Framework Core - 代码优先 - 两个外键 - 一张表 - Entity Framework Core - Code First - Two Foreign Keys - One Table 实体框架代码首先用于具有属性集合的对象 - Entity Framework code first for object with collection of properties 具有实体框架代码的XML字段优先 - XML Fields with Entity Framework Code First 实体框架代码优先的一对一关系 - One to One relationship in Entity Framework Code First 实体框架代码首先一对一可选 - Entity Framework code first one to one optional 实体框架(5.0)代码优先-在Collection中插入Collection - Entity Framework (5.0) Code First - Insert into Collection within Collection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM