简体   繁体   English

对于某些循环实体,多个添加的实体可能具有相同的主键

[英]multiple added entities may have the same primary key for some cyclic entity

I have some cyclic entity, want to save all the details of EntityA by attaching navigation objects like Entity B and EntityC, but when it try to save getting "multiple added entities may have the same primary key error".我有一些循环实体,想通过附加实体 B 和 EntityC 等导航对象来保存 EntityA 的所有详细信息,但是当它尝试保存时得到“多个添加的实体可能具有相同的主键错误”。 Ids in all the table are identity.所有表中的ID都是身份。 Basically I want a reference of A and B in table C and reference of A in B.`基本上我想要表 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; }
      }

Make sure you are adding the same tracked instance reference.确保您添加的是相同的跟踪实例引用。 This type of error is commonly cause when developers assume that two instances with the same ID are interpreted as pointing at the same record so EF will know they are the same thing.当开发人员假设具有相同 ID 的两个实例被解释为指向同一记录时,通常会导致此类错误,以便 EF 知道它们是同一事物。

For instance: (Error)例如:(错误)

var b = new B { Id = bId, A = new A { Id = aId }};
var c = new C { Id = cId, A = new A { Id = aId }};
context.Bs.Add(b);
context.Cs.Add(c);

bA and c.A point to 2 separate instances of an "A" record. bA 和 c.A 指向“A”记录的 2 个单独实例。 (same ID but different instances) EF will encounter this as 2 new A records and try to insert both. (相同的 ID 但不同的实例) EF 将遇到这作为 2 个新的 A 记录并尝试插入两者。

Assuming the "A" in this case is a new record as well:假设这种情况下的“A”也是一个新记录:

var a = new A { Id = aId };
var b = new B { Id = bId, A = a };
var c = new C { Id = cId, A = a };
context.Bs.Add(b);
context.Cs.Add(c);

The key difference in this example is that we instantiate a new single instance of an "A" and then reference that in our new B and C.此示例的主要区别在于我们实例化了“A”的新单个实例,然后在我们的新 B 和 C 中引用它。 When the Context tracks this, they will be pointing to the same instance.当上下文跟踪这个时,它们将指向同一个实例。

If, however, the A instance should already exist in the database, and possibly tracked by the DbContext even:但是,如果 A 实例应该已经存在于数据库中,并且可能由 DbContext 跟踪,甚至:

var a = context.As.Single(a => a.Id == aId);
var b = new B { Id = bId, A = a };
var c = new C { Id = cId, A = a };
context.Bs.Add(b);
context.Cs.Add(c);

This ensures that the A reference that the context should know about as an existing record is loaded and used in the new B and C references.这可确保在新的 B 和 C 引用中加载和使用上下文应该知道的作为现有记录的 A 引用。 If you create a new A even using an ID that already exists, adding that to the Context directly or indirectly through a new B or C will cause EF to treat it as a new record and try to insert it.如果即使使用已经存在的 ID 创建新 A,直接或通过新 B 或 C 间接将其添加到 Context 将导致 EF 将其视为新记录并尝试插入它。 This will result in duplicate PK errors, or worse, silent issues if the IDs are set up as Identity columns as it can insert new "A" records with new IDs that you don't expect.如果将 ID 设置为身份列,这将导致重复的 PK 错误,或者更糟糕的是,静默问题,因为它可以插入具有您不期望的新 ID 的新“A”记录。

暂无
暂无

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

相关问题 多个添加的实体可能在实体框架中具有相同的主键 - Multiple added entities may have the same primary key in Entity Framework 实体框架6.1:添加的多个实体可能具有相同的主键 - Entity Framework 6.1: Multiple added entities may have the same primary key 多个添加的实体可能具有相同的主键 - Multiple added entities may have the same primary key EF6:多个添加的实体可能具有相同的主键 - EF6: Multiple added entities may have the same primary key 多个添加的实体可能在数据库种子上具有相同的主键 - multiple added entities may have the same primary key on database seed 无法确定关系的主要终点,多个添加的实体可能具有相同的主键 - Unable to determine the principal end of the relationship, Multiple added entities may have the same primary key 无法确定 X 关系的主端。 多个添加的实体可能具有相同的主键 - Unable to determine the principal end of the X relationship. Multiple added entities may have the same primary key EF 6.多个添加的实体可能具有相同的主键。 错误 - EF 6. Multiple added entities may have the same primary key. Error 无法确定etaxiDataModel关系的主要结尾。 多个添加的实体可以具有相同的主键 - Unable to determine the principal end of the etaxiDataModel relationship. Multiple added entities may have the same primary key 无法确定“ Vehicle_VehicleClass”关系的主要结尾。 多个添加的实体可能具有相同的主键 - Unable to determine the principal end of the 'Vehicle_VehicleClass' relationship. Multiple added entities may have the same primary key
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM