在mvc应用程序中linq对nhibernate和eager加载的问题

时间:2010-11-09 18:12:34

标签: nhibernate linq-to-nhibernate eager-loading

您好 我有以下代码使用带有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: 

3 个答案:

答案 0 :(得分:5)

根据Mike Hadlow的博客文章NHibernate Linq Eager Fetching

  

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

您的问题中的查询是否是您正在执行的完全查询?

答案 1 :(得分: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

答案 2 :(得分:0)

显然,这在NH 3.1:https://nhibernate.jira.com/browse/NH-2502

中得到修复