title: Entity Framework generates DateTime in T-SQL with too many digits
body: <p>I'm using the Entity Framework 5 and I've got a column <code>[Updated]</code> in a table of type <code>DateTime</code>. When I set <code>Updated</code> in a new entity (ID = 0) to let's say <code>DateTime.UtcNow</code>, Entity Framework generates an insert statement with a parameter e.g. </p>
  
  <pre><code>'2000-01-01 12:59:59.9991111'
  </code></pre>
  
  <p>This causes an error.</p>
  
  <p>The error thrown by Entity Framework is ridiculus: <em>"DbUpdateConcurrencyException: Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries.</em>" However, I've obtained the query from an SQL Server Profiler, taken out the insert statement from an sp_executesql and then SQL Server gave me the real error: <em>"Msg 241, Level 16, State 1, Line 1: Conversion failed when converting date and/or time from character string."</em></p>
  
  <p>I found that if I change the value to </p>
  
  <pre><code>'2000-01-01 12:59:59.999'
  </code></pre>
  
  <p>then the insert statement generated by Entity Framework works correctly.</p>
  
  <p>I've tried a modulo when setting the <code>Updated</code> to set the last four digits to zero, but then Entity Framework produces <code>'2000-01-01 12:59:59.9990000'</code> which still causes an error. </p>
  
  <p>Do you know how to force the Entity Framework to not generate those invalid zeros?</p>
  
  <p>Regards</p>
  
  <hr>
  
  <h2>Update:</h2>
  
  <p>I found that although Entity Framework uses 7 decimal places, it casts the date string as <code>datetime2</code> and then that <code>datetime2</code> is casted to <code>datetime</code>. This is why the decimal places doesn't cause the error as I suspected while inserting raw parameter values into insert statement (I didn't make a cast to <code>datetime2</code>). The cause of error has been found in trigger which interfere with <code>scope_identity()</code> and <code>@@row_count</code>. These values are checked by Entity Framework and in case of failure it throws a <code>DbUpdateConcurrencyException</code>.</p>
  
  <p>The question that appears at the moment is: How to disable concurrency checking when inserting a new row?</p>
  
  <hr>
  
  <h2>Update:</h2>
  
  <p>Database developers solved the issue by generating values to <code>@@row_count</code> and <code>scope_identity()</code> in their trigger. The important thing to remember is also to set a generated column name (<code>ID</code> in my case). It can't be leave anonymous.</p>
  
  <p><strong>The problem was solved by satisfying the Entity Framework requirements from a database level. Be aware of triggers and their side effects when working with Entity Framework.</strong></p>