问题描述
大家好,
我正在使用实体框架4的自我追踪实体!
更新外键并保存时出现以下错误。
用户代码未处理System.InvalidOperationException
Message =违反了Multiplicity约束。 "PI.Entities.DesignationEmployee"关系的"指定"角色具有多重性1或0..1。 源= System.Data.Entity的
 堆栈跟踪:
       at System.Data.Objects.DataClasses.EntityReference`1.VerifyMultiplicityConstraintsForAdd(Boolean applyConstraints)
在System.Data.Objects.DataClasses.RelatedEnd.Add(IEntityWrapper wrappedTarget,布尔applyConstraints,布尔addRelationshipAsUnchanged,布尔relationshipAlreadyExists,布尔allowModifyingOtherEndOfRelationship,布尔forceForeignKeyChanges)
      在System.Data.Objects.ObjectStateManager.CreateRelationship(EntityEntry targetEntry,RelatedEnd relatedEnd,RelationshipWrapper relationshipWrapper,EntityState requestedState)
      在System.Data.Objects.ObjectStateManager.ChangeRelationshipState(EntityEntry sourceEntry,EntityEntry targetEntry,RelatedEnd relatedEnd,EntityState relationshipState)
      在System.Data.Objects.ObjectStateManager.ChangeRelationshipState(Object sourceEntity,Object targetEntity,String relationshipName,String targetRoleName,EntityState relationshipState)
在PI.Entities.ObjectContextExtensions.ApplyChanges [TEntity](ObjectContext context,String entitySetName,TEntity entity)
这在执行以下代码的方法ApplyChanges中发生,
;               //将所有未触动的关系更改回未更改状态
foreach(allRelationships中的var关系)
{
                    context.ObjectStateManager.ChangeRelationshipState(
                     relationship.End0, relationship.End1,
        relationship.AssociationSet.ElementType.FullName,
                         relationship.AssociationEndMembers [1] .name和
       &NBSP ;                EntityState.Unchanged);
             & NBSP;   }
请让我知道为什么会出现这种错误?
感谢&问候,
Kannan
Hi All,
I'm using Self-Tracking Entities of Entity Framework 4 !
I'm getting the following error when updating a foreign key and saving.
System.InvalidOperationException was unhandled by user code
Message=Multiplicity constraint violated. The role 'Designation' of the relationship 'PI.Entities.DesignationEmployee' has multiplicity 1 or 0..1.
Source=System.Data.Entity
StackTrace:
at System.Data.Objects.DataClasses.EntityReference`1.VerifyMultiplicityConstraintsForAdd(Boolean applyConstraints)
at System.Data.Objects.DataClasses.RelatedEnd.Add(IEntityWrapper wrappedTarget, Boolean applyConstraints, Boolean addRelationshipAsUnchanged, Boolean relationshipAlreadyExists, Boolean allowModifyingOtherEndOfRelationship, Boolean forceForeignKeyChanges)
at System.Data.Objects.ObjectStateManager.CreateRelationship(EntityEntry targetEntry, RelatedEnd relatedEnd, RelationshipWrapper relationshipWrapper, EntityState requestedState)
at System.Data.Objects.ObjectStateManager.ChangeRelationshipState(EntityEntry sourceEntry, EntityEntry targetEntry, RelatedEnd relatedEnd, EntityState relationshipState)
at System.Data.Objects.ObjectStateManager.ChangeRelationshipState(Object sourceEntity, Object targetEntity, String relationshipName, String targetRoleName, EntityState relationshipState)
at PI.Entities.ObjectContextExtensions.ApplyChanges[TEntity](ObjectContext context, String entitySetName, TEntity entity)
This happens in the method ApplyChanges when executing the following code,
// Change all the untouched relationship back to unchanged state
foreach (var relationship in allRelationships)
{
context.ObjectStateManager.ChangeRelationshipState(
relationship.End0,
relationship.End1,
relationship.AssociationSet.ElementType.FullName,
relationship.AssociationEndMembers[1].Name,
EntityState.Unchanged);
}
Please let me know why this error occurs ?
Thanks & Regards,
Kannan
推荐答案
这篇关于违反了多重约束。关系'*'的作用'*'具有多重性1或0..1。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!