mvc应用程序中linq到nhibernate和急加载的问题

mvc应用程序中linq到nhibernate和急加载的问题,nhibernate,linq-to-nhibernate,eager-loading,Nhibernate,Linq To Nhibernate,Eager Loading,你好 我有以下代码使用nhibernate 3.0和linq从数据库检索数据- public IQueryable<myEntity> getEntityWithChild { get { return _currentSession.Query<myEntity>().Fetch(c => c.myOtherEntity); } } public IQueryable getEntityWithChild

你好 我有以下代码使用nhibernate 3.0和linq从数据库检索数据-

public IQueryable<myEntity> getEntityWithChild
        {
            get { return _currentSession.Query<myEntity>().Fetch(c => c.myOtherEntity); }
        }
public IQueryable getEntityWithChild
{
获取{return}currentSession.Query().Fetch(c=>c.myOtherEntity);}
}
当我试图将其传递给视图时,我得到了以下错误,这不是很具体。当我不急于使用以下方法加载时,该方法效果良好-

public IQueryable<myEntity> getEntityWithChild
            {
                get { return _currentSession.Query<myEntity>(); }
            }
public IQueryable getEntityWithChild
{
获取{return}currentSession.Query();}
}
但是,每当另一个实体延迟加载时,我都会创建一个单独的查询。以前有没有人见过这个,也许能给我指出正确的方向。 谢谢你的想法

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: 
System.NotSupportedException未由用户代码处理
Message=不支持指定的方法。
来源=NHibernate
堆栈跟踪:
位于NHibernate.Hql.Ast.ANTLR.PolymorphicQuerySourceDetector.GetClassName(IASTNode querySource)
在NHibernate.Hql.Ast.ANTLR.PolymorphicQuerySourceDetector.Process(IASTNode树)中
在NHibernate.Hql.Ast.ANTLR.astpromphicprocessor.Process()中
在NHibernate.Hql.Ast.ANTLR.astpromphicprocessor.Process(IASTNode Ast、ISessionFactoryImplementor factory)
位于NHibernate.Hql.Ast.ANTLR.ASTQueryTranslatorFactory.CreateQueryTranslators(IASTNode Ast、字符串查询标识符、字符串集合角色、布尔值浅型、IDictionary`2过滤器、ISessionFactoryImplementor工厂)
在NHibernate.Hql.Ast.ANTLR.ASTQueryTranslatorFactory.CreateQueryTranslators(字符串查询标识符、IQueryExpression查询表达式、字符串集合角色、布尔浅层、IDictionary`2过滤器、ISessionFactoryImplementor工厂)
在NHibernate.Engine.Query.HQLExpressionQueryPlan.CreateTranslators(字符串表达式str、IQueryExpression queryExpression、字符串集合角色、布尔浅值、IDictionary`2启用过滤器、ISessionFactoryImplementor工厂)
在NHibernate.Engine.Query.HQLExpressionQueryPlan..ctor(字符串表达式str、IQueryExpression queryExpression、字符串集合角色、布尔浅值、IDictionary`2启用过滤器、ISessionFactoryImplementor工厂)
在NHibernate.Engine.Query.HQLExpressionQueryPlan..ctor(字符串表达式str、IQueryExpression queryExpression、布尔浅值、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.NhQueryProvider.PrepareQuery(表达式、IQuery和查询、NhLinqExpression和nhQuery)
在NHibernate.Linq.NhQueryProvider.Execute(表达式)
在NHibernate.Linq.NhQueryProvider.Execute[TResult](表达式)
at Remotion.Data.Linq.QueryableBase`1.GetEnumerator()
位于System.Collections.Generic.List`1..ctor(IEnumerable`1集合)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
在C:\Users\carl.PAMB.000\Documents\Visual Studio 2010\Projects\Calendar\Calendar\Controllers\HomeController.cs中的vCalWebCalendar.Controllers.homeControllers()中:第41行
在lambda_方法中(闭包、控制器基、对象[])
位于System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller,Object[]参数)
位于System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext ControllerContext,IDictionary`2参数)
位于System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext ControllerContext,ActionDescriptor ActionDescriptor,IDictionary`2参数)
在System.Web.Mvc.ControllerActionInvoker.c_uudisplayClassD.b_uua()中
位于System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter筛选器、ActionExecutingContext预文本、Func`1 continuation)
内部异常:

根据Mike Hadlow的博客帖子

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


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

看起来像是当前LINQ提供程序的错误或限制。Fetch()子句必须是链中的最后一个方法,即使在Select()之后也是如此。因此产生了上述例外情况:

_currentSession.Query<myEntity>().Fetch(c => c.myOtherEntity).Select(x => x);
\u currentSession.Query().Fetch(c=>c.myOtherEntity)。选择(x=>x);
鉴于此项工作:

_currentSession.Query<myEntity>().Select(x => x).Fetch(c => c.myOtherEntity);
\u currentSession.Query().Select(x=>x.Fetch(c=>c.myOtherEntity);
由于您返回的是IQueryable,我怀疑您正在筛选或选择应用程序堆栈中更高的位置,这将导致.Fetch()引发异常。Mike Hadlow在他的博客上有更多关于这种烦恼的信息:


很明显,NH3.1中固定了这一点:

是的,这就是确切的查询。(实体名称显然已更改,但查询顺序相同。)是否确定?不要忘记,由于这是一个IQueryable,执行将被推迟,您在模型或视图中对它所做的任何操作都将被视为原始查询的一部分。一种快速的测试方法是使
getEntityWithChild
方法返回
IEnumerable
。如果我将该方法更改为返回IEnumerable,它确实有效。我在控制器里做一个过滤器。我没有意识到,通过使用IQueryable,我在控制器中所做的一切都会影响原始查询。我从控制器中删除了我的过滤器,只使用了原始查询,它工作得非常好。谢谢你。如果你需要选择y,那该怎么办。但是你还想得到x的一些属性吗_currentSession.Query().Select(x=>y).Fetch(???);