【发布时间】:2020-08-02 15:34:25
【问题描述】:
我有一些循环实体,想通过附加导航对象来保存 EntityA 的所有细节 像 Entity B 和 EntityC 一样,但是当它尝试保存时得到“多个添加的实体可能有相同的主键错误”。所有表中的ID都是身份。基本上我想在表 C 中引用 A 和 B,在 B 中引用 A。`
public class EntityA
{
[Key]
public int QID { get; set; }
public virtual List<EntityC> EntityCs { get; set; }
public virtual List<EntityB> EntityBs { get; set; }
}
public class EntityB
{
[Key]
public int OptionID { get; set; }
public int QID { get; set; }
[ForeignKey("QID")]
public virtual EntityA EntityA { get; set; }
}
public class EntityC
{
[Key]
public int CID { get; set; }
public int QID { get; set; }
public int OptionID { get; set; }
[ForeignKey("QID")]
public virtual EntityA EntityA { get; set; }
[ForeignKey("OptionID")]
public virtual EntityB Option { get; set; }
}
【问题讨论】:
-
我做了什么,我尝试在下面插入,因为 id 列是身份所以没有通过这个:Entity A a = new EntityA(); a.EntityBS = new List
(); a.EntityCs = new List 那样添加记录序列();然后 context.EntityAs.Add(a); context.saveChanges();但是出现错误,我认为 EF 没有像第一个 A--->B---->C
标签: c# asp.net-mvc entity-framework ef-code-first