Derik Whittaker

Syndication

News


Hey, NHibernate give me back my milliseconds

Have you ever noticed that out of the box NHibernate’s DateTime type will truncate/ignore your milliseconds for DateTime fields?  If you do not believe me check out this post.  If you think about why it does this it will become clear, NHibernate runs against MANY databases and each one of them stores DateTime values slightly different.  However all of them store basic YYYY-MM-DD : HH:MM:SS.

If you google how to resolve this issue you will find a few a post that suggests you create a custom User Type (IUserType).  Although I am sure this will work, this just seems like such overkill for the problem.

What I was able to do was setup my mapping in FluentNHibernate as such

Map( x => x.MyField, "My_Field" ).CustomTypeIs("timestamp");

By adding the .CustomTypeIs attribute (i tried CustomSqlTypeIs but this did not work) NHibernate was able to save my DateTime property into a DateTime (not Timestamp or DateTime2) field with the milliseconds inplace.

Hope this helps,

Till next time,


Posted 02-22-2010 4:47 PM by Derik Whittaker

[Advertisement]

Comments

Chad Myers wrote re: Hey, NHibernate give me back my milliseconds
on 02-22-2010 6:29 PM

Note that in SQL Server, the Milliseconds part is only accurate to 300ths of a second (+/- a few)

Do not rely on milliseconds in SQL Server for anything important because, basically, all rows inserted within a 300ms timeframe (which could be many) will all have the exact same time stamp.

More info:

stackoverflow.com/.../why-is-sql-server-losing-a-millisecond

Derik Whittaker wrote re: Hey, NHibernate give me back my milliseconds
on 02-22-2010 7:35 PM

@Chad,

yea we know of this, but we have an existing need which rely's on milliseconds and something is better than all .000 's

Damien Guard wrote re: Hey, NHibernate give me back my milliseconds
on 02-22-2010 8:45 PM

If it's using the ADO.NET command objects, which it should be, then the datetime should be parameterized and the provider should take care of all the precision for you.

It sounds like it's embedding it as a constant into the SQL...

[)amien

Frank Mao wrote re: Hey, NHibernate give me back my milliseconds
on 02-23-2010 1:42 PM

We use DateTime as the version column, fortunately milliseconds is still there. I think we just need to notify NH we want our datetime acting as timestamp.

 Version(x => x.LastModifiedDate)

               .Column("S_Date")

               .Generated.Always()

               ;

Ramon Smits wrote re: Hey, NHibernate give me back my milliseconds
on 05-11-2010 9:50 AM

This is also mentioned in the docs and also works in *.xml.hbm files

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)