By RichardHowells on
17 May 2011
I wanted a very simple logging table. I wanted it to be time ordered and I decided to include a smalldatetime column in the primary key.
This failed badly with Entity Framework. Inserts resulted in a failure message indicating that zero rows were affected. This because a .Net DateTime value is not guaranteed to round trip into SqlServer and back. The SqlServer datetime (and smalldatetime) column types cannot store values with the same precision as the .Net DateTime type.
EF uses a query like this  to store the row and recover the freshly inserted identity column. The DateTime value is ‘rounded’ to suit SQL Server as it is inserted and the Select fails to read the freshly inserted row back because the datetime values are no longer equal.
I solved this by removing the datetime column from the primary key. It was not crucial for it to be in the primary key.
Moral – do not mix datetime and identity columns in your primary key.
 Entity Framework Generated Query
Read More »