gpt4 book ai didi

c# - NHibernate 无法匹配 DateTime 对象

转载 作者:行者123 更新时间:2023-11-29 11:00:35 32 4
gpt4 key购买 nike

我在 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,它提示:

"Input string '10:00:00' was not in the correct format.

还有

Unable to cast object of type 'System.TimeSpan' to type 'System.IConvertible'

MySQL 数据库中的列的类型为 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; }
}

最佳答案

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

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

关于c# - NHibernate 无法匹配 DateTime 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42356905/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com