Peter Kiss
Peter Kiss

Reputation: 9319

Multiplicity constraint violated

I have two entites:

Configuration:

modelBuilder.Entity<ClaimDetailStatus>()
    .HasRequired<ClaimDetail>(x => x.ClaimDetail)
    .WithMany(x => x.ClaimDetailStatus)
    .HasForeignKey(x => x.ClaimDetailID).WillCascadeOnDelete(true);

modelBuilder.Entity<ClaimDetail>()
    .HasMany<ClaimDetailStatus>(x => x.ClaimDetailStatus)
    .WithRequired(x => x.ClaimDetail)
    .HasForeignKey(x => x.ClaimDetailID).WillCascadeOnDelete(true);

When i'm writing the code, running & debug it everything is fine but when i want to save everything it throws an exception:

System.InvalidOperationException was unhandled by user code
  HResult=-2146233079
  Message=Multiplicity constraint violated. The role 'ClaimDetailStatus_ClaimDetail_Target' of the relationship 'EPer.DataAccess.ClaimDetailStatus_ClaimDetail' has multiplicity 1 or 0..1.
  Source=System.Data.Entity
  StackTrace:
       at System.Data.Objects.DataClasses.EntityReference`1.AddToLocalCache(IEntityWrapper wrappedEntity, Boolean applyConstraints)
       at System.Data.Objects.EntityEntry.TakeSnapshotOfSingleRelationship(RelatedEnd relatedEnd, NavigationProperty n, Object o)
       at System.Data.Objects.EntityEntry.TakeSnapshotOfRelationships()
       at System.Data.Objects.DataClasses.RelatedEnd.AddEntityToObjectStateManager(IEntityWrapper wrappedEntity, Boolean doAttach)
       at System.Data.Objects.DataClasses.RelatedEnd.AddGraphToObjectStateManager(IEntityWrapper wrappedEntity, Boolean relationshipAlreadyExists, Boolean addRelationshipAsUnchanged, Boolean doAttach)
       at System.Data.Objects.DataClasses.RelatedEnd.IncludeEntity(IEntityWrapper wrappedEntity, Boolean addRelationshipAsUnchanged, Boolean doAttach)
       at System.Data.Objects.DataClasses.EntityCollection`1.Include(Boolean addRelationshipAsUnchanged, Boolean doAttach)
       at System.Data.Objects.DataClasses.RelationshipManager.AddRelatedEntitiesToObjectStateManager(Boolean doAttach)
       at System.Data.Objects.ObjectContext.AddObject(String entitySetName, Object entity)
       at System.Data.Entity.Internal.Linq.InternalSet`1.ActOnSet(Action action, EntityState newState, Object entity, String methodName)
       at System.Data.Entity.Internal.Linq.InternalSet`1.Add(Object entity)
       at System.Data.Entity.DbSet`1.Add(TEntity entity)
       at EPer.DataAccess.ObjectSetAdapter`1.AddObject(T entity) in c:\KBData\_repo\Intranet\Apps\EOffice\EPer\EPer.Dal\ObjectSetAdapter.cs:line 41
       at EPer.BusinessLogic.DomainServices.ClaimComponent.CreateClaim(Claim claimModel, UserClaim uc) in c:\KBData\_repo\Intranet\Apps\EOffice\EPer\EPer.BusinessLogic\DomainServices\ClaimComponent.cs:line 28
       at EPer.BusinessLogic.ApplicationServices.ClaimService.CreateClaim(Claim claimModel, IEnumerable`1 autoApprovers, UserClaim uc) in c:\KBData\_repo\Intranet\Apps\EOffice\EPer\EPer.BusinessLogic\ApplicationServices\ClaimService.cs:line 180
       at EPer.BusinessLogic.ApplicationServices.ClaimService.CreateClaim(Claim claimModel, IEnumerable`1 autoApprovers) in c:\KBData\_repo\Intranet\Apps\EOffice\EPer\EPer.BusinessLogic\ApplicationServices\ClaimService.cs:line 113
       at EPer.BusinessLogic.ApplicationServices.ClaimService.CreateClaim(Claim claimModel) in c:\KBData\_repo\Intranet\Apps\EOffice\EPer\EPer.BusinessLogic\ApplicationServices\ClaimService.cs:line 99
       at EPer.Areas.Claims.Controllers.ClaimController.Create(SingleClaim m) in c:\KBData\_repo\Intranet\Apps\EOffice\EPer\EPer\Areas\Claims\Controllers\ClaimController.cs:line 107
       at lambda_method(Closure , ControllerBase , Object[] )
       at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
       at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
       at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass42.<BeginInvokeSynchronousActionMethod>b__41()
       at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass37.<>c__DisplayClass39.<BeginInvokeActionMethodWithFilters>b__33()
       at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49()
  InnerException: 

Generated association:

<Association Name="ClaimDetailStatus_ClaimDetail">
  <End Role="ClaimDetailStatus_ClaimDetail_Source" Type="Self.ClaimDetailStatus" Multiplicity="*" />
  <End Role="ClaimDetailStatus_ClaimDetail_Target" Type="Self.ClaimDetail" Multiplicity="1">
    <OnDelete Action="Cascade" />
  </End>
  <ReferentialConstraint>
    <Principal Role="ClaimDetailStatus_ClaimDetail_Target">
      <PropertyRef Name="ID" />
    </Principal>
    <Dependent Role="ClaimDetailStatus_ClaimDetail_Source">
      <PropertyRef Name="ClaimDetailID" />
    </Dependent>
  </ReferentialConstraint>

I have checked the generated visualized Entity Data Model and everything looked okay.

What is happening here?

EDIT correction

I have a 3rd entity: Claim; if a Claim contains only one ClaimDetail everything works fine but if it contains 2 ClaimDetail the above exception is thrown.

Upvotes: 1

Views: 7520

Answers (2)

Saul Jml
Saul Jml

Reputation: 1

ClaimDetail (int ID, IList ClaimDetailStatus, other...)
ClaimDetailStatus (int ID, ClaimDetailClaimDetail, other...)

This happened when I got entity from db, then tried to save the edited one.

Try

Detached ClaimDetail.ClaimDetailStatus.ClaimDetailClaimDetail,
ClaimDetail.ClaimDetailStatus,
ClaimDetail

Then save, it works.

Upvotes: 0

Peter Kiss
Peter Kiss

Reputation: 9319

The problem was that i added to each of the ClaimDetail's ClaimDetailStatus collection the same instance of the first ClaimDetailStatus. If i'm adding different instances (in a cycle detail.ClaimDetailStatus.Add(new ...)) everything works fine.

Upvotes: 3

Related Questions