title: Replace entity in context with a different instance of the same entity
body: <p>I have an entity which is not connected to my <code>dbcontext</code>. I want to change that. However there is already another instance of the same entity attached to <code>dbcontext</code>. If I just add my new entity, I get an error, that an entity with the same primary key is already attached.<br>
  I tried multiple different variants of removing the old entity from <code>dbcontext</code> without any success. How can I replace the old instance with the new one?<br>
  Note: I don't want to copy the values, I want to attach this very instance of my new entity to <code>dbcontext</code>.</p>
  
  <pre><code>var entity = new MyEntity { Id = 1 };
  var logicalDuplicate = dbcontext.Set&lt;MyEntity &gt;().Local
      .FirstOrDefault(e =&gt; e.Equals(entity));
  if (logicalDuplicate != null)
  {
      // remove logicalDuplicate from dbcontext
  }
  dbcontext.MyEntity.Attach(entity);
  </code></pre>
  
  <p>For clarification: I have overridden <code>Equals</code> to check for <code>Id</code> instead of reference.</p>