Mappin Sql-Time to TimeSpan with NHibernate
May 12, 2011 by Daniel HoelblingFunny how long you can use NHibernate + Fluent NHibernate on greenfield applications and not use all of it’s mapping features. But one little project that needs to talk to a legacy database and you run into all kinds of troubles.
Today I had to map the time
datatype present in MsSql2008. According to official Microsoft documentation the preferred clr-type for time
is TimeSpan
.
Unfortunately, by default NHibernate believes that the best way to map TimeSpan
is to transform it to a bigint
.
Fortunately the solution is rather trivial:
Map(p => p.CreatedOn, "CreationTime")
.CustomType("TimeAsTimeSpan");