title: Deleting an entity with concurrency in EF Core
body: <p>I want to delete an entity in EF Core without loading it first from the database. I know similar questions have been asked before, but bear with me, because this case is different. Additional to the usual ID, the entity also has a row version, which is causing problems.</p>
  
  <p>The entity is defined like this:</p>
  
  <pre><code>public int MyEntity {
      public int Id { get; set; }
      //Other irrelevant properties
      public byte[] RowVersion { get; set; }
  }
  </code></pre>
  
  <p>The entity is configured with the fluent API:</p>
  
  <pre><code>class MyEntityConfiguration : IEntityTypeConfiguration&lt;MyEntity&gt; {
      public void Configure( EntityTypeBuilder&lt;MyEntity&gt; builder ) {
          builder.Property( e =&gt; e.RowVersion )
              .IsRequired()
              .IsRowVersion();
      }
  }
  </code></pre>
  
  <p>The row version allows me to do optimistic concurrency checking. The problem is that when I try to delete the entity without loading it first like this...</p>
  
  <pre><code>void RemoveMyEntity( int id ) {
      MyEntity removeEntity = new MyEntity {
          Id = id
      };
      m_context.MyEntities.Attach( removeEntity );
      m_context.MyEntities.Remove( removeEntity );
  }
  </code></pre>
  
  <p>...the concurrency check falls on my feet. I get this error message in a <code>DbUpdateConcurrencyException</code>:</p>
  
  <blockquote>
    <p>Database operation expected to affect 1 row(s) but actually affected 0 row(s). Data may have been modified or deleted since entities were loaded.</p>
  </blockquote>
  
  <p>The reason is that EF Core generates this query to delete the item:</p>
  
  <pre><code>exec sp_executesql N'SET NOCOUNT ON;
  DELETE FROM [MyEntity]
  WHERE [Id] = @p57 AND [RowVersion] IS NULL;
  SELECT @@ROWCOUNT',N'@p57 int',@p57=1 -- &lt;--The ID of the item to delete
  </code></pre>
  
  <p>The issue is clearly in <code>AND [RowVersion] IS NULL</code>. This condition can <em>never</em> be <code>true</code>, since (like I have clearly told EF when configuring the entity), the column is required and hence can not be <code>NULL</code>.</p>
  
  <p>Of course, I have not added a row version in the entity I want to delete and I actually don't want to add the row version, because this would mean I have to get the data from the DB, which is not necessary in this case. I don't even mind concurrency checking here, because it does not hurt if the item was deleted before.</p>
  
  <p>So the question is: Is there a way to ignore concurrency checking for this operation (but not for other operations in the same transaction) or to make the delete work in another way without having to get the row version from the DB first.</p>