title: Updating object with related entities from detached state
body: <p>When I query from the entity framework I always query in a detached state so that the records retrieved can be stored in cache for subsequent requests.</p>
  
  <p>Right now I have a form that the user can edit which contains a parent record, and then two lists of parent records.</p>
  
  <p>When the data is POSTed to the server, I take my view models and map them into the entity framework objects using AutoMapper.  The data looks fine; AutoMapper is mapping the data correctly.</p>
  
  <p>When I attach the object so that I can update it, an exception is thrown:  <code>A referential integrity constraint violation occurred: The property values that define the referential constraints are not consistent between principal and dependent objects in the relationship</code>.</p>
  
  <pre><code>public static void UpdateOrder(ShippingOrder shippingOrder) {
      using (OrderEntity orderContext = new OrderEntity()) {
          //Exception happens here
          orderContext.ShippingOrders.Attach(shippingOrder);
          //Update the order itself; mark the order has being modified so the EF will update it.
          orderContext.ObjectStateManager.ChangeObjectState(shippingOrder, System.Data.EntityState.Modified);
          //Perform the update.
          orderContext.SaveChanges();
      }
  }
  </code></pre>
  
  <p>The EntityFramework (EF) seems to think that my keys aren't lining up, but I'm not sure what isn't correct.  The foreign key property does have the correct value, so I'm not sure what it's checking.  Does anyone have any ideas?</p>