小编典典

如何使用NHibernate处理来自SQL Server 2008的TIME数据类型?

sql

我正在使用TIMESQL Server 2008中的数据类型,但是在使其与NHibernate一起使用时遇到一些问题。

public TimeTableEventMap()
{
    Id(x => x.Id)
    Map(x => x.Day).NvarcharWithMaxSize().Not.Nullable();
    Map(x => x.StartTime).Length(4).TimeDataType().Not.Nullable();
    Map(x => x.Endtime).Length(4).TimeDataType().Not.Nullable();
    References(x => x.TimeTable).Not.Nullable().Cascade.All();
    References(x => x.RequiredSettings).Not.Nullable().Cascade.All();
}

/// <summary>
///  MS Sql 2008 date type.
/// </summary>
/// <param name="map"></param>
/// <returns></returns>
public static PropertyPart TimeDataType(this PropertyPart map)
{
   return map.CustomSqlType("time");
}

public class TimeTableEvent
{
    public virtual int Id { get; private set; }
    public virtual DayOfWeek Day { get; set; }
    public virtual DateTime StartTime { get; set; }
    public virtual DateTime Endtime { get; set; }
    public virtual TimeTable TimeTable { get; set; }
    public virtual RequiredSetting RequiredSettings { get; set; }
}

我得到这个错误

NHibernate.Exceptions.GenericADOException被捕获
消息=无法执行查询
[SELECT TOP(@
p0)this_.TimeTableEventId作为TimeTabl1_15_1_,this_.Day作为Day15_1_,this_.StartTime作为StartTime15_1_,this_.Endtime作为Endtime15_1_,this_.TimeTableId作为TimeTabl5_15_1
this_.RequiredSettingsId如Required6_15_1_,requiredse2_.RequiredSettingsId如Required1_10_0_,requiredse2_.BackgroundColor如Backgrou2_10_0_,requiredse2_.Title如Title10_0_
FROM TimeTableEvents THIS_内部联接RequiredSettings
requiredse2_上this_.RequiredSettingsId = requiredse2_.RequiredSettingsId
WHERE this_.TimeTableId(P1 @)]在
位置参数:#0> 14
[SQL:SELECT TOP(@
p0)this_.TimeTableEventId作为TimeTabl1_15_1_,this_.Day作为Day15_1_,this_.StartTime作为StartTime15_1_,this_.Endtime作为Endtime15_1_,this_.TimeTableId作为TimeTabl5_15_1_,this_.RequiredSettingsId作为Required6_15_1_,
,requiredse2_.BackgroundColor作为Backgrou2_10_0_,requiredse2_.Title作为Title10_0_
FROM TimeTableEvents this_内部联接required_settings this_.RequiredSettingsId =
requiredse2_.RequiredSettingsId上的requiredse2_。在(@ p1)中this_.TimeTableId的位置]
Source = NHibernate
SqlString = SELECT TOP(@
p0)this_.TimeTableEventId作为TimeTabl1_15_1_,this_.Day作为Day15_1_,this_.StartTime作为StartTime15_1_,this_.Endtime作为Endtime15_1_,this_.TimeTableId作为TimeTabl5_15_1_,this_.RequiredSettingsId作为Required6_15_1,
requiredse2_.BackgroundColor为Backgrou2_10_0_,requiredse2_.Title为Title10_0_
FROM TimeTableEvents this_内部联接Required_settings this_.RequiredSettingsId =
requiredse2_.RequiredSettingsId上的requiredse2_在(@ p1)中的this_.TimeTableId

堆栈跟踪:
在NHibernate.Loader.Loader.DoList(ISessionImplementor会话,QueryParameters
queryParameters)

在NHibernate.Loader.Loader.ListIgnoreQueryCache(ISessionImplementor会话,QueryParameters
queryParameters)
在NHibernate.Loader.Loader.List(ISessionImplementor会话,QueryParameters
queryParameters,ISet的1。 ListU
在NHibernate.Criterion.QueryOver1.NHibernate.IQueryOver.ListU 在TimeTableRepo.cs:线47 在TimeTableService.cs:行43 的InnerException信息:System.FormatException 消息=输入字符串'16:00:00'是不正确格式: Source = NHibernate StackTrace: 位于NHibernate.Type.DateTimeType.Get(IDataReader rs,Int32索引)1 querySpaces, IType[] resultTypes)
at NHibernate.Loader.Criteria.CriteriaLoader.List(ISessionImplementor
session)
at NHibernate.Impl.SessionImpl.List(CriteriaImpl criteria, IList results)
at NHibernate.Impl.CriteriaImpl.List(IList results)
at NHibernate.Impl.CriteriaImpl.ListT
at NHibernate.Criterion.QueryOver`

在NHibernate.Type.AbstractType.Hydrate(IDataReader
rs,NHibernate.Type.NullableType.NullSafeGet(IDataReader rs,String name)
在NHibernate.Type.NullableType.NullSafeGet(IDataReader rs,String
[]名称,ISessionImplementor会话,对象所有者)
在NHibernate.Type.AbstractType.Hydrate(IDataReader rs,
NHibernate的String
[]名称,ISessionImplementor会话,对象所有者)在NHibernate的NHibernate.Persister.Entity.AbstractEntityPersister.Hydrate(IDataReader
rs,Object id,Object obj,ILoadable rootLoadable,String [] []
suffixedPropertyColumns,Boolean allProperties,ISessionImplementor会话)
。 Loader.Loader.LoadFromResultSet(IDataReader rs,Int32
i,对象obj,字符串instanceClass,EntityKey键,字符串rowIdAlias,LockMode
lockMode,ILoadable rootPersister,ISessionImplementor会话)
在NHibernate.Loader.Loader.InstanceNotYetLoaded(IDataReader的博士的Int32
I,ILoadable持留,的EntityKey键,LockMode lockMode,字符串rowIdAlias,的EntityKey
optionalObjectKey,对象optionalObject,IList的hydratedObjects,ISessionImplementor会话)
在NHibernate.Loader.Loader.GetRow(IDataReader的RS ,ILoadable []持久性,EntityKey
[]键,Object optionalObject,EntityKey optionalObjectKey,LockMode []
lockModes,IList hydredObjects,ISessionImplementor session)
位于NHibernate.Loader.Loader.GetRowFromResultSet(IDataReader
resultSet,ISessionImplementor session,QueryParameters
queryParameters,LockMode [] ,EntityKey optionalObjectKey,IList
hydredObjects,EntityKey []键,布尔值returnProxies)
在NHibernate.Loader.Loader.DoQuery(ISessionImplementor会话,QueryParameters
queryParameters,布尔returnProxies)
处,
在NHibernate.LoaderLoader.Loader.Loader。 )
InnerException:System.InvalidCastException
Message =无法将类型为“ System.TimeSpan”的对象转换为类型为“ System.IConvertible”的对象。
来源= mscorlib

public List<TimeTableEvent> GetTimeTableEvents(Student student, List<int> timeTableIds)
{
    TimeTableEvent tAlias = null;

    List<TimeTableEvent> allEvents = session.QueryOver<TimeTableEvent>(() => tAlias)
        .Where(Restrictions.In(Projections.Property(() => tAlias.TimeTable.Id), timeTableIds))
        .Fetch(r => r.RequiredSettings).Eager
        .TransformUsing(Transformers.DistinctRootEntity)
        .Take(QueryLimits.TimeTableEvents)
        .List<TimeTableEvent>().ToList();

    return allEvents;
}

阅读 231

收藏
2021-04-14

共1个答案

小编典典

NHibernate中的日期/时间支持

2021-04-14