title: How can i ignore a DbUpdateConcurrencyException with my Entity Framework code?
body: <p><strong>Is there any way I can tell EF to not worry about the number of rows a <code>DELETE</code> or <code>UPDATE</code> do or don't do?</strong></p>
  <p>I'm trying to delete a row from the database, but because the row doesn't exist, EF throws an exception: <code>DbUpdateConcurrencyException</code> .. saying <em>0 rows were affected</em>. This is right -&gt; no rows were deleted. But that's totally fine .. cause there's no data.</p>
  <p>I don't really want to do a round-trip to the DB to see if that row exists .. and if so .. then try and delete it.</p>
  <p>If i try and swallow the exception in a <code>try / catch</code> block, then the rest of the items to be deleted do NOT get sent to the db, when I try to <code>SaveChanges()</code> ... which is bad.</p>
  <p>eg.</p>
  <pre><code>Delete(new Foo(1));
  Delete(new Foo(2));
  Delete(new Foo(3));
  SaveChanges(); // &lt;-- Throws the exception.
  
  // DB Trace : DELETE FROM Foo WHERE Id = 1;
  </code></pre>
  <p>and thats it.. there's no trace showing record 2 or 3 trying to get deleted .. because the exception stops everything :(</p>
  <p>Any ideas?</p>
  <h3>UPDATE</h3>
  <p>How does <code>Delete</code> work? Here's the code... (simplified and strongly typed)</p>
  <pre><code>public void Delete(Foo foo)
  {
      if (foo == null)
      {
          throw new ArgumentNullException(&quot;foo&quot;);
      }
  
      Foo attachedEntity = Context.Set&lt;Foo&gt;().Local.FirstOrDefault(x =&gt; x.Id &gt; 0);
  
      if (attachedEntity != null)
      {
          // Entity already in object graph - remove entity.
          Context.Set&lt;Foo&gt;().Remove(attachedEntity);
      }
      else
      {
          // Entity not in object graph, attach and set EntityState to Deleted.
          Context.Entry(foo).State = EntityState.Deleted;
      }
  }
  </code></pre>