NHibernate在匹配DateTime对象时遇到问题

时间:2017-02-21 01:02:39

标签: c# mysql asp.net datetime nhibernate

我在ASP.NET MVC中有这个简单的操作

    [HttpGet]
    public IEnumerable<EventDTO> Get(int bed, DateTime date)
    {
        using (var session = DBSessionFactory.OpenSession())
        {
            return session.Query<Event>()
                .Where(e =>
                    e.Bed.Id == bed
                    && e.Date == date)
                .ToList()
                .Select(x => Mapper.Map<EventDTO>(x))
                .ToList();
        }
    }

使用NHibernate,它抱怨:

  

&#34;输入字符串&#39; 10:00:00&#39;格式不正确。

  

无法转换类型为#System; TimeSpan&#39;的对象输入&#39; System.IConvertible&#39;

MySQL DB中的列是DATETIME类型。我之前使用的是EF6,之后转换为NH。这是完成过渡的最后一道障碍。知道是什么导致NH绊倒了吗?谢谢!

编辑:

这是调用堆栈:

   at NHibernate.Loader.Loader.DoList(ISessionImplementor session, QueryParameters queryParameters, IResultTransformer forcedResultTransformer)
   at NHibernate.Loader.Loader.DoList(ISessionImplementor session, QueryParameters queryParameters)
   at NHibernate.Loader.Loader.ListIgnoreQueryCache(ISessionImplementor session, QueryParameters queryParameters)
   at NHibernate.Loader.Loader.List(ISessionImplementor session, QueryParameters queryParameters, ISet`1 querySpaces, IType[] resultTypes)
   at NHibernate.Loader.Hql.QueryLoader.List(ISessionImplementor session, QueryParameters queryParameters)
   at NHibernate.Hql.Ast.ANTLR.QueryTranslatorImpl.List(ISessionImplementor session, QueryParameters queryParameters)
   at NHibernate.Engine.Query.HQLQueryPlan.PerformList(QueryParameters queryParameters, ISessionImplementor session, IList results)
   at NHibernate.Impl.SessionImpl.List(IQueryExpression queryExpression, QueryParameters queryParameters, IList results)
   at NHibernate.Impl.AbstractSessionImpl.List(IQueryExpression queryExpression, QueryParameters parameters)
   at NHibernate.Impl.AbstractQueryImpl2.List()
   at NHibernate.Linq.DefaultQueryProvider.ExecuteQuery(NhLinqExpression nhLinqExpression, IQuery query, NhLinqExpression nhQuery)
   at NHibernate.Linq.DefaultQueryProvider.Execute(Expression expression)
   at NHibernate.Linq.DefaultQueryProvider.Execute[TResult](Expression expression)
   at Remotion.Linq.QueryableBase`1.GetEnumerator()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at OEETracker.Controllers.Api.EventsController.Get(Int32 bed, DateTime date) in EventsController.cs:line 27
   at lambda_method(Closure , Object , Object[] )
   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters)
   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)
   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)

事件类:

public class Event
{
    public virtual int Id { get; set; }
    public virtual DateTime Date { get; set; }
    public virtual TimeSpan StartTime { get; set; }
    public virtual TimeSpan EndTime { get; set; }
    public virtual bool Planned { get; set; }
    public virtual string EngineSN { get; set; }
    public virtual string Details { get; set; }
    public virtual EventType Type { get; set; }
    public virtual Classification Classification { get; set; }

    public virtual Bed Bed { get; set; }
    public virtual Subcategory Subcategory { get; set; }
    public virtual Project Project { get; set; }
}

1 个答案:

答案 0 :(得分:1)

DateTime/Time/DateTimeOffset/Date数据类型上,我总是明确告诉映射中的预期类型,因为从底层属性类型推断时可能存在一些歧义(.Net缺少Date类型,... )。

通常对于db DateTime,我在属性映射上指定NHibernate类型timestamp。请参阅可用类型列表on NHibernate reference documentation。根据您的使用情况,有许多可供选择的类型 您还可以指定一些非NHibernate类型,如in NHibernate reference documentation所述。