title: Multiplicity constraint violated. The role '...' of the relationship '...' has multiplicity 1 or 0..1
body: <p>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."</p>
  
  <p>using ASP.NET, Entity Framework 4</p>
  
  <p>Working with a detached entity  </p>
  
  <p>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. </p>
  
  <p>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. </p>
  
  <p>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. </p>
  
  <p>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.</p>
  
  <p>The code (very simplified) is something like this:</p>
  
  <pre><code>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=&gt;x.id == detachedEntity.id);       
  
  
     // &lt;remove children representing lookup table elements from detachedEntity to prevent duplicates&gt;
     // &lt;remove children representing view elements from detachedEntity to prevent duplicates&gt;
  
  
     // &lt;apply changes from detachedEntity to attachedEntity&gt;
  
  
     // &lt;add new children&gt;
     // &lt;remove deleted children&gt;
     // &lt;update modified children&gt;
  
  
     // &lt;set entity state to unchanged on view and lookup elements of attachedEntity to ensure no duplicates...&gt;
  
  
     // &lt;validate&gt;
  
  
     if (errors.count&gt;0)
       // &lt;report errors&gt;
     else
       context.SaveChanges();
  }
  }
  </code></pre>
  
  <p>as an example, this generates a multiplicity error:</p>
  
  <pre><code>// 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=&gt;x.id == detachedEntity.id);       
  
         int debug1 = context.ChangeTracker.Entries&lt;ChildEntity&gt;().Count(); // debug1 == 0;
  
         attachedEntity.ChildEntities.Add(detachedEntity.ChildEntities.First());
  
         int debug2 = context.ChangeTracker.Entries&lt;ChildEntity&gt;().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=&gt;x.id == detachedEntity.id);    
  
         int debug1 = context.ChangeTracker.Entries&lt;ChildEntity&gt;().Count(); // debug1 == 0;
  
         attachedEntity.ChildEntities.Add(detachedEntity.ChildEntities.First());
  
         int debug2 = context.ChangeTracker.Entries&lt;ChildEntity&gt;().Count(); // multiplicity error;
  
      }
  </code></pre>