[NHibernate] DateTime without milliseconds, how to increase precision
Software which I was updating into XXI century had database in which every record contains time stamp about insert or latest update.
In NHibernate – DateTime is saved with precision of seconds. “Old” software was saving with precision of milliseconds, it was requirement to hold that precision.
Help came from StackOverflow.
public class DateTimeMilisecondsConventions : IPropertyConvention
{
public void Apply(IPropertyInstance instance)
{
Type type = instance.Property.PropertyType;
if (type == typeof (DateTime) || type == typeof (DateTime?))
{
instance.CustomType("Timestamp");
}
}
}
After defining class like that we need to include it to configuration. I use FleuntNHibernate so config look like this:
Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2012
.ConnectionString(c => c.FromAppSetting("ConnectionString")))
.Mappings(m =>
{
m.FluentMappings.AddFromAssemblyOf();
m.FluentMappings.Conventions.AddFromAssemblyOf();
})
.BuildSessionFactory();