我建议将TimeSpan 用于DB 类型Time
public class TimeSlot : EntityBase
{
public virtual TimeSpan FromTime { get; set; }
public virtual TimeSpan ToTime { get; set; }
}
然后 NHibernate 确实有一个特殊的类型来处理这个技巧:
Map(c => c.FromTime)
.Type<NHibernate.Type.TimeAsTimeSpanType>();
...
这将允许您使用 .NET 本机“类似时间”类型 -
表示时间间隔。
如果我们更喜欢使用DateTime,NHibernate 可以使用它来表示时间,我们必须这样做:
public class TimeSlot : EntityBase
{
public virtual DateTime FromTime { get; set; }
public virtual DateTime ToTime { get; set; }
}
现在我们可以使用问题中的类型 - NHibernate.Type.TimeType
Map(c => c.FromTime)
.Type<NHibernate.Type.TimeType>();
...
描述是:
/// <summary>
/// Maps a <see cref="System.DateTime" /> Property to an DateTime column that only stores the
/// Hours, Minutes, and Seconds of the DateTime as significant.
/// Also you have for <see cref="DbType.Time"/> handling, the NHibernate Type <see cref="TimeAsTimeSpanType"/>,
/// the which maps to a <see cref="TimeSpan"/>.
/// </summary>
/// <remarks>
/// <para>
/// This defaults the Date to "1753-01-01" - that should not matter because
/// using this Type indicates that you don't care about the Date portion of the DateTime.
/// </para>
/// <para>
/// A more appropriate choice to store the duration/time is the <see cref="TimeSpanType"/>.
/// The underlying <see cref="DbType.Time"/> tends to be handled differently by different
/// DataProviders.
/// </para>
/// </remarks>
[Serializable]
public class TimeType : PrimitiveType, IIdentifierType, ILiteralType
同时检查:
Date/Time Support in NHibernate
... 与时间相关的 DbTypes 只存储时间,但不存储日期。在 .NET 中,没有 Time 类,因此 NHibernate 使用 DateTime 并将日期组件设置为 1753-01-01,SQL 日期时间或 System.TimeSpan 的最小值 - 取决于我们选择的 DbType...