entity framework代码优先 – 将两个字段联合成一个集合
我有这个型号和配置
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 Childs { get; set; } } class PersonConfiguration : EntityTypeConfiguration { 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); } }
我得到这个类型是初始的错误。
指定的架构无效。 错误:(151,6):错误0040:类型Person_Father未在命名空间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 ChildrenAsFather { get; set; } public virtual List ChildrenAsMother { get; set; } } class PersonConfiguration : EntityTypeConfiguration { 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); } }
谢谢你,Eranga,你的回复正是我所需要的!
另外,如果有人使用该方法而不是Eranga使用的配置方法,这里是modelBuilder代码。
上述就是C#学习教程:entity framework代码优先 – 将两个字段联合成一个集合分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity(). HasKey(i => i.PersonId); modelBuilder.Entity (). HasOptional(f => f.Father). WithMany(f => f.ChildrenAsFather). HasForeignKey(f => f.FatherId); modelBuilder.Entity (). HasOptional(m => m.Mother). WithMany(m => m.ChildrenAsMother). HasForeignKey(m => m.MotherId); }
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1020350.html