SQL Server DateTime precision and NHibernate

Posted: Fri 3 Sep, 2010

When storing a DateTime value into SQL server via NHibernate, you'll likely be storing the value in a column with the DateTime data type.  

This is fine for many purposes, however if  you take note of the Ticks property of a c# DateTime variable before saving to the database, and after getting it back out, you will notice that the values are different and you have lost some precision.

It is possible to overcome this by using the DateTime2 data type introduced in SQL Server 2008 (NHibernate supports this via the 2008 dialect) or by storing the value as a BigInt. 

Blog comments powered by Disqus