重新审视这一点,因为以前的答案有些不完整。
假设您希望以完整详细信息和毫秒精度存储所有 DateTime 字段,请TIMESTAMP(6) WITH TIME ZONE
用作 SQL 列类型。您需要 FluentNHibernate 的属性约定:
using System;
using FluentNHibernate.Conventions;
using FluentNHibernate.Conventions.AcceptanceCriteria;
using FluentNHibernate.Conventions.Inspections;
using FluentNHibernate.Conventions.Instances;
internal class DateTimeMapsToTimestampConvention
: IPropertyConvention, IPropertyConventionAcceptance
{
public void Apply(IPropertyInstance instance)
{
instance.CustomType<NHibernate.Type.TimestampType>();
}
public void Accept(IAcceptanceCriteria<IPropertyInspector> criteria)
{
criteria.Expect(x => x.Type == typeof(DateTime)
|| x.Type == typeof(DateTime?));
}
}
然后将此约定添加到流式配置会话工厂构建代码中:
var factory = Fluently.Configure().Mappings(
m => assemblies.ForEach(
assembly => m.FluentMappings.AddFromAssembly(assembly)
.Conventions.Add(new DateTimeMapsToTimestampConvention())))
.BuildSessionFactory();