NHibernate Pitfalls: DateTime Type Loses Milliseconds

This is part of a series of posts about NHibernate Pitfalls. See the entire collection here.

By default, when you have a mapped DateTime property on an entity, if you don’t specify any type, NHibernate will use the NHibernate.Type.DateTimeType type. Unfortunately, with this type, you lose the milliseconds.

The workaround is to specify type NHibernate.Type.TimestampType:

   1: public class SomeClassMapping : ClassMapping<SomeClass>
   2: {
   3:     public SomeClassMapping()
   4:     {        
   5:         //...
   6:  
   7:         this.Property(x => x.Timestamp, x =>
   8:         {
   9:             x.Column("timestamp");
  10:             x.NotNullable(true);
  11:             x.Type(NHibernateUtil.Timestamp);
  12:         });
  13:     }
  14: }

                             

No Comments