2

您好,我有以下代码可以使用带有 linq 的 nhibernate 3.0 从我的数据库中检索数据-

public IQueryable<myEntity> getEntityWithChild
        {
            get { return _currentSession.Query<myEntity>().Fetch(c => c.myOtherEntity); }
        }

当我尝试将其传递给视图时,我收到以下不是很具体的错误。当我不急于使用以下加载时,这工作正常 -

public IQueryable<myEntity> getEntityWithChild
            {
                get { return _currentSession.Query<myEntity>(); }
            }

但是每次其他实体延迟加载时,我都会创建一个单独的查询。有没有人在此之前看到过这可能会指出我正确的方向。感谢您的任何想法。

System.NotSupportedException was unhandled by user code
  Message=Specified method is not supported.
  Source=NHibernate
  StackTrace:
       at NHibernate.Hql.Ast.ANTLR.PolymorphicQuerySourceDetector.GetClassName(IASTNode querySource)
       at NHibernate.Hql.Ast.ANTLR.PolymorphicQuerySourceDetector.Process(IASTNode tree)
       at NHibernate.Hql.Ast.ANTLR.AstPolymorphicProcessor.Process()
       at NHibernate.Hql.Ast.ANTLR.AstPolymorphicProcessor.Process(IASTNode ast, ISessionFactoryImplementor factory)
       at NHibernate.Hql.Ast.ANTLR.ASTQueryTranslatorFactory.CreateQueryTranslators(IASTNode ast, String queryIdentifier, String collectionRole, Boolean shallow, IDictionary`2 filters, ISessionFactoryImplementor factory)
       at NHibernate.Hql.Ast.ANTLR.ASTQueryTranslatorFactory.CreateQueryTranslators(String queryIdentifier, IQueryExpression queryExpression, String collectionRole, Boolean shallow, IDictionary`2 filters, ISessionFactoryImplementor factory)
       at NHibernate.Engine.Query.HQLExpressionQueryPlan.CreateTranslators(String expressionStr, IQueryExpression queryExpression, String collectionRole, Boolean shallow, IDictionary`2 enabledFilters, ISessionFactoryImplementor factory)
       at NHibernate.Engine.Query.HQLExpressionQueryPlan..ctor(String expressionStr, IQueryExpression queryExpression, String collectionRole, Boolean shallow, IDictionary`2 enabledFilters, ISessionFactoryImplementor factory)
       at NHibernate.Engine.Query.HQLExpressionQueryPlan..ctor(String expressionStr, IQueryExpression queryExpression, Boolean shallow, IDictionary`2 enabledFilters, ISessionFactoryImplementor factory)
       at NHibernate.Engine.Query.QueryPlanCache.GetHQLQueryPlan(IQueryExpression queryExpression, Boolean shallow, IDictionary`2 enabledFilters)
       at NHibernate.Impl.AbstractSessionImpl.GetHQLQueryPlan(IQueryExpression queryExpression, Boolean shallow)
       at NHibernate.Impl.AbstractSessionImpl.CreateQuery(IQueryExpression queryExpression)
       at NHibernate.Linq.NhQueryProvider.PrepareQuery(Expression expression, IQuery& query, NhLinqExpression& nhQuery)
       at NHibernate.Linq.NhQueryProvider.Execute(Expression expression)
       at NHibernate.Linq.NhQueryProvider.Execute[TResult](Expression expression)
       at Remotion.Data.Linq.QueryableBase`1.GetEnumerator()
       at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
       at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
       at vCalWebCalendar.Controllers.HomeController.Hearings() in C:\Users\carl.PAMB.000\Documents\Visual Studio 2010\Projects\Calendar\Calendar\Controllers\HomeController.cs:line 41
       at lambda_method(Closure , ControllerBase , Object[] )
       at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
       at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
       at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
       at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClassd.<InvokeActionMethodWithFilters>b__a()
       at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
  InnerException: 
4

3 回答 3

5

根据 Mike Hadlow 的博文NHibernate Linq Eager Fetching

请注意,如果您想将 Fetch 与其他子句混合使用,Fetch 必须始终排在最后。

您问题中的查询是您正在执行的确切查询吗?

于 2010-11-09T19:07:16.067 回答
4

看起来像是当前 LINQ 提供程序的错误或限制。Fetch() 子句必须是链中的最后一个方法——即使在 Select() 之后也是如此。所以这会产生上述异常:

_currentSession.Query<myEntity>().Fetch(c => c.myOtherEntity).Select(x => x);

而这有效:

_currentSession.Query<myEntity>().Select(x => x).Fetch(c => c.myOtherEntity);

由于您要返回 IQueryable,我怀疑您正在过滤或选择应用程序堆栈中更高的位置,这将导致 .Fetch() 引发异常。Mike Hadlow 在他的博客上有更多关于这个烦恼的信息:

http://mikehadlow.blogspot.com/2010/08/nhibernate-linq-eager-fetching.html

于 2010-11-09T19:09:32.630 回答
0

显然这是在 NH 3.1 中修复的:https ://nhibernate.jira.com/browse/NH-2502

于 2011-03-19T03:20:46.677 回答