代码之家  ›  专栏  ›  技术社区  ›  czuroski

mvc应用中的linq-to-nhibernate和急加载问题

  •  2
  • czuroski  · 技术社区  · 15 年前

    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 回复  |  直到 15 年前
        1
  •  5
  •   Chris Shouts    15 年前

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

    你的问题是 准确的

        2
  •  4
  •   James Kovacs    15 年前

    看起来像是当前LINQ提供程序的一个bug或限制。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