C# 在Fluent Nhibernate中使用时间类型会生成异常“;无法强制转换类型为';System.DateTime';输入';NHibernate.Type.TimeType“;

C# 在Fluent Nhibernate中使用时间类型会生成异常“;无法强制转换类型为';System.DateTime';输入';NHibernate.Type.TimeType“;,c#,nhibernate,fluent-nhibernate,nhibernate-mapping,C#,Nhibernate,Fluent Nhibernate,Nhibernate Mapping,我发现NHibernate有几种内置类型,它们不存在于C中,但存在于一些SGBD中 现在我有以下几点: public class TimeSlot : EntityBase { public virtual NHibernate.Type.TimeType FromTime { get; set; } public virtual NHibernate.Type.TimeType ToTime { get; set; } } public class T

我发现NHibernate有几种内置类型,它们不存在于
C
中,但存在于一些SGBD中

现在我有以下几点:

public class TimeSlot : EntityBase
{            
    public virtual NHibernate.Type.TimeType FromTime { get; set; }
    public virtual NHibernate.Type.TimeType ToTime { get; set; }
}

public class TimeSlotMap : ClassMap<TimeSlot>
{
    public TimeSlotMap()
    {
        Id(c => c.Id).GeneratedBy.Identity();
        Map(c => c.FromTime);
        Map(c => c.ToTime);                     
    }
}
公共类时隙:EntityBase
{            
公共虚拟NHibernate.Type.TimeType FromTime{get;set;}
公共虚拟NHibernate.Type.TimeType ToTime{get;set;}
}
公共类TimeSlotMap:ClassMap
{
公共TimeSlotMap()
{
Id(c=>c.Id).GeneratedBy.Identity();
Map(c=>c.FromTime);
Map(c=>c.ToTime);
}
}
在MSSQL中,此表如所附图像所示

现在,当我试图查询此表时,我遇到以下异常:

无法将“System.DateTime”类型的对象强制转换为“NHibernate.type.TimeType”


我做错了什么?Fluent NHibernate是如何处理时间-日期类型的?

我建议,对DB类型使用
TimeSpan
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>();
...
现在我们可以使用问题中的类型-
NHibernate.type.TimeType

Map(c => c.FromTime)
   .Type<NHibernate.Type.TimeType>();
...
Map(c=>c.FromTime)
.Type();
...
哪种描述是:

/// <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
//
///将属性映射到仅存储
///DateTime的小时、分钟和秒具有重要意义。
///还有一种是NHibernate类型,
///映射到的。
/// 
/// 
/// 
///这将默认日期设置为“1753-01-01”-这不重要,因为
///使用此类型表示您不关心DateTime的日期部分。
/// 
/// 
///存储持续时间/时间的更合适选择是。
///潜在的问题往往由不同的人来处理
///数据提供者。
/// 
/// 
[可序列化]
公共类时间类型:PrimitiveType、IIIdentifierType、ILiteralType
同时检查:

。。。与时间相关的DbTypes只存储时间,不存储日期。在.NET中,没有时间类,因此NHibernate使用日期组件设置为1753-01-01的DateTime,这是SQL DateTime或System.TimeSpan的最小值-取决于我们选择的数据库类型


感谢您的回答,我已经在使用TimeSpan来处理时间数据类型。再次感谢。如果这有帮助,那就太好了。。。享受NHibernate,很棒的工具:)
/// <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