title: Inconsistent Object State Error In Entity Framework When Saving
body: <p>I have the following code to simulate an owner selling his dog to someone else and the dog getting a new name:</p>
  
  <pre><code>public void ChangeOwner(int oldOwnerID, int newOwnerID, int dogID, string newDogName)
  {
      var oldOwner = _objectContext.People.FirstOrDefault(x =&gt; x.ID == oldOwnerID);
      oldOwner.DogID = null;
  
      var newOwner = _objectContext.People.FirstOrDefault(x =&gt; x.ID == newOwnerID);
      newOwner.DogID = dogID;
  
      var dog = _objectContext.Dogs.FirstOrDefault(x =&gt; x.ID == dogID);
      dog.Name = newDogName;
  
      _objectContext.SaveChanges();
  }
  </code></pre>
  
  <p>This code will throw the following error:</p>
  
  <p>Additional information: The changes to the database were committed successfully, but an error occurred while updating the object context. The ObjectContext might be in an inconsistent state. Inner exception message: AcceptChanges cannot continue because the object's key values conflict with another object in the ObjectStateManager. Make sure that the key values are unique before calling AcceptChanges.</p>
  
  <p>Why is this error occuring and what can I do to fix it?</p>