title: Optimistic Concurrency
body: <p>I have an Entity Framework Project with several linked entities. Since it is utilized by multiple users at once I've set up a RowVersion-Field for entities which are likely to be edited by several users at once. Unfortunately I now get an <code>OptimisticConecurrencyException</code> every time I try to save a new entity, which is linked to an already existing entity. </p>
  
  <blockquote>
    <p>Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. See <a href="http://go.microsoft.com/fwlink/?LinkId=472540" rel="nofollow">http://go.microsoft.com/fwlink/?LinkId=472540</a> for information on understanding and handling optimistic concurrency exceptions.</p>
  </blockquote>
  
  <p>The problem is now that this error doesn't really give any pointers as to where the error really lies. It could either be the underlying model that is modified in the meantime, there could be a validation error on the new model or something else.</p>
  
  <p>The code I use to add the new entity is as follows:</p>
  
  <pre><code>using (ctx = new DbContext())
  {
      try
      {
          ctx.Samples.Add(model);
          ctx.SaveChanges();
      }
      catch (DbUpdateConcurrencyException ex)
      {
          LogManager.HandleException(ex.InnerException);
      }
  }
  </code></pre>
  
  <p><code>model</code> is the model i want to add to the database</p>
  
  <p>Edit: As seen above i modified the code to ignore the update of an underlying model. Furthermore i have verified through:</p>
  
  <pre><code>ctx.Database.log = s =&gt; Debug.Write(s);
  </code></pre>
  
  <p>That only an insert statement is sent to the database and not an additional update statement.</p>
  
  <pre><code>INSERT [dbo].[Samples]([IDSample], [ModificationDate], [IDUser])
  VALUES (@0, @1, @2) 
  SELECT [RowVersion]
  FROM [dbo].[Samples]
  WHERE @@ROWCOUNT &gt; 0 AND [IDSample] = @0 AND [ModificationDate] = @1
  </code></pre>
  
  <p>I would understand the exception if i would update an entity and the rowversion column wouldn't match, but in this case it's a completely new entity. Is there a way to see if one of the properties is malformed?</p>
  
  <p>Edit2:</p>
  
  <p>Instead of just trimming the milliseconds i now used DateTime.Today instead of DateTime.Now which works. Seemingly there is some problem with datetime2(4) on ModificationDate. I already made sure that ModificationDate is truncated to 4 milliseconds so there should be no parse error.</p>
  
  <p>Edit3:</p>
  
  <p>After switching back to DateTime.Now and trimming the milliseconds it stopped working and the entities are not longer inserted into the database. Could this be caused by the fact that the sql server has problems matching the entities based on millisecond values. I executed the EF generated SQL as seen above with some fictional values and it went through although on some occasions the query didn't return a rowversion-value. In terms of the entity framework, the client would interpret this as a return value of 0 lines and therefore call an concurrency-exception. (It should also be of note that the ModificationDate together with the IDSample is the primary key of the entity.)</p>
  
  <p>Edit4:</p>
  
  <p>I'm now using DateTime.Today and then add the needed precision, which works for me. This can be flagged as solved. (Altough i would have expected that EF can take care of datetime-format-conversion by itself :/)</p>