Entity Framework: Remove and Add entities with same key in a single request
<p>I need to Remove and Add entities with the same primary key value in a single request, can anybody suggest me the solution?</p>

<p>Below is my sample code that gives the error: <strong>Violation of PRIMARY KEY constraint 'PK_Table'. Cannot insert duplicate key in object 'dbo.Table'</strong>.</p>

<pre><code>context.Set&lt;Entity&gt;().Attach(existingEntityObj);
Entry(existingEntityObj).State = EntityState.Deleted;

context.Set&lt;Entity&gt;().Add(newEntityObj);
context.Entry&lt;Entity&gt;(newEntityObj).State = EntityState.Added;

context.SaveChanges();
</code></pre>

<p>Assume both the objects (existingEntityObj and newEntityObj) have the same value in the primary key property.</p>

<p>thanks in advance!!</p>