多重约束侵犯。的关系中的作用“......”“......”有多重1或0..1 [英] Multiplicity constraint violated. The role '...' of the relationship '...' has multiplicity 1 or 0..1

查看:488
本文介绍了多重约束侵犯。的关系中的作用“......”“......”有多重1或0..1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到从我的DbContext以下错误:。多重约束违反关系MyModel.FK_ChildEntities_MyEntities'的作用myEntity所'有多重1或0..1

使用ASP.NET

,实体框架4

与分离实体工作

错误发生第二次我尝试重新连接的实体到的DbContext。保存其次是重新尝试的情况是不成功的。

我在会议上分离实体。用户更改属性的表单,添加的东西,删除的东西,最后点击保存。我得到的实体从的DbContext的新实例附加拷贝,应用从分离实体变更到连接实体,验证,发现一个错误并中止。用户改变一切并再次保存。

在第二保存,整个保存过程重复,只是这一次它都去地狱。 pretty多少都被复制,造成一人错误或其他或全部。从意见和查找表中的值的只应该是引用创造了新的和重新分配的ID。大多数的这些问题我已经能够解决的,但我留下了多重错误。子元素被创建为其他子元素,只在Added状态的精确副本下来的唯一ID。或者,如果我引用的某些属性,而不是克隆未修改的孩子,它丢弃了新的。无论哪种方式,没有任何code是因为它没有围绕在第一时间执行。

我丢弃的DbContext和所附的实体各自保存尝试的实例。我认为这将足以恢复任何变化,但必须有所坚持左右。唯一不discared或复位是独立的实体,这是在会话,但我不作出任何更改。至少没有直接。

在code(非常简单)是这样的:

 无效保存()
{
使用(VAR上下文=新MyContext())
{
   //从会话分离实体
   myEntity所detachedEntity =(myEntity所)会议[DetachedEntity];   //从上下文连接实体
   myEntity所attachedEntity = context.MyEntities.Single(X => x.id == detachedEntity.id);
   //<清除孩子重新presenting查找表元素从detachedEntity至prevent重复>
   //<清除孩子重新presenting视图元素从detachedEntity至prevent重复>
   //<申请从detachedEntity更改attachedEntity>
   //<添加新的儿童>
   //<清除删除的子>
   //<更新修改的子>
   //<设置实体状态在视图和查找attachedEntity元素不变,以确保没有重复...>
   //<&验证GT;
   如果(errors.count大于0)
     //<报告错误>
   其他
     context.SaveChanges();
}
}

作为一个例子,这会产生一个多重错误:

  //重新presents先救:
    使用(VAR上下文=新MyContext())
    {
       //从会话分离实体
       myEntity所detachedEntity =(myEntity所)会议[DetachedEntity];       //从上下文连接实体
       myEntity所attachedEntity = context.MyEntities.Single(X => x.id == detachedEntity.id);       INT DEBUG1 = context.ChangeTracker.Entries< ChildEntity方式>()计数(); // DEBUG1 == 0;       attachedEntity.ChildEntities.Add(detachedEntity.ChildEntities.First());       INT DEBUG2 = context.ChangeTracker.Entries< ChildEntity方式>()计数(); // DEBUG2 == 1;    }//重新presents的第二次节省:
    使用(VAR上下文=新MyContext())
    {
       //从会话分离实体
       myEntity所detachedEntity =(myEntity所)会议[DetachedEntity];       //从上下文连接实体
       myEntity所attachedEntity = context.MyEntities.Single(X => x.id == detachedEntity.id);       INT DEBUG1 = context.ChangeTracker.Entries< ChildEntity方式>()计数(); // DEBUG1 == 0;       attachedEntity.ChildEntities.Add(detachedEntity.ChildEntities.First());       INT DEBUG2 = context.ChangeTracker.Entries< ChildEntity方式>()计数(); //多重错误;    }


解决方案

不知何故的DbContext记得加入其中哪些对象。如果完全相同的对象显示出来两次,它...吹

而不是从我的独立实体,增加孩子的实体连接的一个,我应该已经创建每个孩子的新副本

  ChildEntity detachedChild = detachedEntity.ChildEntities.First();
attachedEntity.ChildEntities.Add(新ChildEntity {
   propertyA = detachedChild.propertyA,
   propertyB = detachedChild.propertyB
});

而不是

  attachedEntity.ChildEntities.Add(detachedEntity.ChildEntities.First());

I'm getting the following error from my DbContext: "Multiplicity constraint violated. The role 'MyEntity' of the relationship 'MyModel.FK_ChildEntities_MyEntities' has multiplicity 1 or 0..1."

using ASP.NET, Entity Framework 4

Working with a detached entity

The error happens the second time I try to reattach an entity to the dbcontext. The scenario is an unsuccessful save followed by a reattempt.

I have a detached entity in session. The user changes properties in a form, add things, removes things and finally clicks save. I get an attached copy of the entity from a new instance of the dbcontext, apply changes from the detached entity to the attached entity, validate, find an error and abort. The user changes whatever and saves again.

On the second save, the whole save process repeats, only this time it all goes to hell. Pretty much everything is duplicated, causing one error or another or all of them. Values from views and lookup tables that are only supposed to be references are created new and reassigned id's. Most of those issues I've been able to resolve, but I'm left with the multiplicity error. Child elements are being created as exact copies of other child elements, down to the unique id, only in the Added state. Or, if I reference certain properties, instead of cloning an unmodified child, it drops the new one. Either way, none of the code is executing as it did the first time around.

I'm discarding the instance of the dbcontext and the attached entity each save attempt. I thought that would be enough to revert any changes but something must be sticking around. The only thing not discared or reset is the detached entity, which is in session, but I dont make any changes to it. At least not directly.

The code (very simplified) is something like this:

void Save()
{
using (var context = new MyContext())
{
   // detached entity from session
   MyEntity detachedEntity = (MyEntity)Session["DetachedEntity"];

   // attached entity from context
   MyEntity attachedEntity = context.MyEntities.Single(x=>x.id == detachedEntity.id);       


   // <remove children representing lookup table elements from detachedEntity to prevent duplicates>
   // <remove children representing view elements from detachedEntity to prevent duplicates>


   // <apply changes from detachedEntity to attachedEntity>


   // <add new children>
   // <remove deleted children>
   // <update modified children>


   // <set entity state to unchanged on view and lookup elements of attachedEntity to ensure no duplicates...>


   // <validate>


   if (errors.count>0)
     // <report errors>
   else
     context.SaveChanges();
}
}

as an example, this generates a multiplicity error:

// represents first save:
    using (var context = new MyContext())
    {
       // detached entity from session
       MyEntity detachedEntity = (MyEntity)Session["DetachedEntity"];

       // attached entity from context
       MyEntity attachedEntity = context.MyEntities.Single(x=>x.id == detachedEntity.id);       

       int debug1 = context.ChangeTracker.Entries<ChildEntity>().Count(); // debug1 == 0;

       attachedEntity.ChildEntities.Add(detachedEntity.ChildEntities.First());

       int debug2 = context.ChangeTracker.Entries<ChildEntity>().Count(); // debug2 == 1;

    }

// represents second save:
    using (var context = new MyContext())
    {
       // detached entity from session
       MyEntity detachedEntity = (MyEntity)Session["DetachedEntity"];

       // attached entity from context
       MyEntity attachedEntity = context.MyEntities.Single(x=>x.id == detachedEntity.id);    

       int debug1 = context.ChangeTracker.Entries<ChildEntity>().Count(); // debug1 == 0;

       attachedEntity.ChildEntities.Add(detachedEntity.ChildEntities.First());

       int debug2 = context.ChangeTracker.Entries<ChildEntity>().Count(); // multiplicity error;

    }

解决方案

somehow the dbcontext remembers what objects were added to it. if the exact same object shows up twice, it... blows

instead of adding child entities from my detached entity to the attached one, i should've been creating new copies of each child

ChildEntity detachedChild = detachedEntity.ChildEntities.First();
attachedEntity.ChildEntities.Add(new ChildEntity { 
   propertyA = detachedChild.propertyA,
   propertyB = detachedChild.propertyB
});

instead of

attachedEntity.ChildEntities.Add(detachedEntity.ChildEntities.First());

这篇关于多重约束侵犯。的关系中的作用“......”“......”有多重1或0..1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆