title: Unable to attach a detached entity: "An object with the same key already exists in the ObjectStateManager"
body: <p>I am trying to attach an entity to the ObjectContext.
  When I do so, the following InvalidOperationException is thrown:</p>
  
  <pre><code>An object with the same key already exists in the ObjectStateManager.
  The ObjectStateManager cannot track multiple objects with the same key.
  </code></pre>
  
  <p>I checked in the object state manager and the item does not exist:</p>
  
  <pre><code>//Data context is actually the object context.
  ObjectStateEntry contact;
  while ( //Should only work once since it should be true if the item was attached
            !DataContext.ObjectStateManager.
            TryGetObjectStateEntry(Contact, out contact)
        )
        DataContext.Attach(Contact); //Here is the exception thrown.
  </code></pre>
  
  <p>Or look at this abstract example and tell me if it makes sense:</p>
  
  <pre><code>EntityState state = Contact.EntityState; //Detached
  
  DataContext.Attach(Contact); //Throws the exception.
  DataContext.AttachTo("Entities.Contacts", Contact); //Throws the Exception
  
  var detached = DataContext.ObjectStateManager.
                     GetObjectStateEntries(EntityState.Detached);
  //InvalidArgumentException - detached entities cannot be in the obj state mgr
  </code></pre>
  
  <p>Answers in VB are welcomed too.</p>