流利的NHibernate NotSupportedException

时间:2017-11-15 11:59:56

标签: c# linq nhibernate fluent-nhibernate

我在尝试使用 NHibernate 4.1.0 实现查询时得到System.NotSupportedException

  

NHibernate.dll中发生了'System.NotSupportedException'类型的异常但未在用户代码中处理

我从一个非常简单的LINQ查询开始(如果需要可以提供实体结构):

var query = session.Query<Attendee>()
  .Select(a => new AttendeeViewModel
  {
    Id = a.Id, // long
    FirstName = a.FirstName, // string
    LastName = a.LastName, // string
    Attended = a.Attended, // bool
    BookingId = a.ParentBooking.Id, // long
    SessionName = a.ParentBooking.ParentSession.Name, // string
    SessionDate = a.ParentBooking.ParentSession.SessionDate, // DateTime
  });

然后将过滤条件解析为表达式并应用.Where()

foreach (var filter in request.filters)
{
  var param = Expression.Parameter(typeof(AttendeeViewModel));
  var prop = Expression.Property(param, filter.PropName);
  var value = Expression.Constant(filter.Value);
  var comparison = Expression.Equal(prop, value);
  var filterExpression = Expression.Lambda<Func<AttendeeViewModel, bool>>(comparison, param);
  query = query.Where(filterExpression);
}

当我实现以下时,System.NotSupportedException被抛出:

var result = query.ToList();

让我感到困惑的一点是,在对Id以外的任何属性进行过滤时,这样可以正常工作,即:

filter.PropName = "Id";
filter.Value = 1234;

我无法进入NHibernate代码以查看导致异常的确切原因,并且异常中包含的信息似乎没有任何相关性。

  

System.NotSupportedException未被用户代码
处理   的HResult = -2146233067
  Message = new AttendeeViewModel(){Id = [100001] .Id,FirstName = [100001] .FirstName,LastName = [100001] .LastName,Attended = [100001] .Attended,BookingId = [100001] .Id,SessionName = [ 100001] .Name,SessionDate = [100001] .SessionDate}
  来源= NHibernate的
   堆栈跟踪:
         at NHibernate.Linq.Visitors.HqlGeneratorExpressionTreeVisitor.VisitExpression(Expression expression)
         at NHibernate.Linq.Visitors.HqlGeneratorExpressionTreeVisitor.VisitMemberExpression(MemberExpression expression)
         在NHibernate.Linq.Visitors.HqlGeneratorExpressionTreeVisitor.VisitBinaryExpression(BinaryExpression表达式)
         在NHibernate.Linq.Visitors.QueryModelVisitor.VisitWhereClause(WhereClause whereClause,QueryModel queryModel,Int32 index)
         在Remotion.Linq.QueryModelVisitorBase.VisitBodyClauses(ObservableCollection`1 bodyClauses,QueryModel queryModel)
         在Remotion.Linq.QueryModelVisitorBase.VisitQueryModel(QueryModel queryModel)
         在NHibernate.Linq.Visitors.QueryModelVisitor.GenerateHqlQuery(QueryModel queryModel,VisitorParameters参数,布尔根)
         在NHibernate.Linq.NhLinqExpression.Translate(ISessionFactoryImplementor sessionFactory,布尔过滤器)
         在NHibernate.Hql.Ast.ANTLR.ASTQueryTranslatorFactory.CreateQueryTranslators(IQueryExpression queryExpression,String collectionRole,Boolean shallow,IDictionary`2过滤器,ISessionFactoryImplementor工厂)
         在NHibernate.Engine.Query.QueryPlanCache.GetHQLQueryPlan(IQueryExpression queryExpression,布尔浅,IDictionary`2 enabledFilters)
         在NHibernate.Impl.AbstractSessionImpl.GetHQLQueryPlan(IQueryExpression queryExpression,布尔浅)
         在NHibernate.Impl.AbstractSessionImpl.CreateQuery(IQueryExpression queryExpression)
         在NHibernate.Linq.DefaultQueryProvider.PrepareQuery(表达式表达式,IQuery&amp; query,NhLinqExpression&amp; nhQuery)
         在NHibernate.Linq.DefaultQueryProvider.Execute(表达式表达式)
         在NHibernate.Linq.DefaultQueryProvider.Execute [TResult](表达式)          at ###。GetAttendeePageData(FilterServiceModel`1 filterServiceModel)in ###。cs:line 134
         at ###。GetAttendeePageData(FilterServiceModel`1 filterServiceModel)in ###。svc.cs:line 36
         在SyncInvokeGetAttendeePageData(Object,Object [],Object [])
         在System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(对象实例,对象[]输入,对象[]和输出)
         在System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc&amp; rpc)

0 个答案:

没有答案