【问题标题】:Enumeration error on results fetched using nhibernate session create sql query使用 nhibernate 会话创建 sql 查询获取的结果的枚举错误
【发布时间】:2012-03-21 05:53:27
【问题描述】:

我使用的是尖拱 1.0 版。

在 NHibernate PostUpdateEvent 中我试图访问数据库。

public class PostUpdateListener : IPostUpdateEventListener 
{ 
    public void OnPostUpdate(PostUpdateEvent postUpdateEvent) 
    { 
        var session = NHibernateSession.Current;

        var results = session.CreateSQLQuery("Select * from Storefront").List<object>();
        for (int i = 0; i < results.Count; i++)
        {
        }
  }

当我尝试保存任何实体并在 postupdateevent 中运行此选择查询时,它会在 OnFlush 上给出枚举错误。 NHibernate\Listeners\FlushFixEventListener .cs 行:35

我读到使用 foreach 循环运行枚举操作,所以最好运行 for 循环。但我尝试了 for 循环。仍然没有区别。

使用 SharArch NHibernate Transaction 属性处理保存操作。如果我删除 Transaction 属性,则 postupdatelistener 中的查询可以正常工作。

这是堆栈跟踪。

[InvalidOperationException:集合已修改;枚举操作可能无法执行。] System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource 资源)+56 System.Collections.Generic.Enumerator.MoveNextRare() +58 System.Collections.Generic.Enumerator.MoveNext() +93 NHibernate.Engine.ActionQueue.ExecuteActions(IList 列表) 在 d:\horn.horn\orm\nhibernate\Working-2.1\src\NHibernate\Engine\ActionQueue.cs:112 NHibernate.Engine.ActionQueue.ExecuteActions() 在 d:\horn.horn\orm\nhibernate\Working-2.1\src\NHibernate\Engine\ActionQueue.cs:147 NHibernate.Event.Default.AbstractFlushingEventListener.PerformExecutions(IEventSource session) 在 d:\horn.horn\orm\nhibernate\Working-2.1\src\NHibernate\Event\Default\AbstractFlushingEventListener.cs:241 NHibernate.Event.Default.DefaultFlushEventListener.OnFlush(FlushEvent 事件) 在 d:\horn.horn\orm\nhibernate\Working-2.1\src\NHibernate\Event\Default\DefaultFlushEventListener.cs:19 D:\Solutions\Infrastructure\NHibernate\Listeners\FlushFixEventListener .cs:35 中的 Infrastructure.NHibernate.Listeners.FlushFixEventListener.OnFlush(FlushEvent 事件) NHibernate.Impl.SessionImpl.Flush() 在 d:\horn.horn\orm\nhibernate\Working-2.1\src\NHibernate\Impl\SessionImpl.cs:1478 Infrastructure.NHibernate.LinqRepository1.Save(T entity) in D:\Solutions\Infrastructure\NHibernate\LinqRepository.cs:95 Tasks.Shared.ContentEntityTasks4.Save(TSaveEntityRequestDetails details) 在 D:\Solutions\Tasks\Shared\ContentEntityTasks.cs:96 D:\Solutions\Web.Controllers\Entity\EntityController.cs:379 中的 Web.Controllers.Entity.EntityController.Edit(EntityViewModel entityViewModel, HttpPostedFileBase fileName, HttpPostedFileBase mainImageFileName, HttpPostedFileBase thumbnailFileName) lambda_method(ExecutionScope, ControllerBase, Object[]) +185 System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary2 parameters) +236 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary2个参数) +31 System.Web.Mvc.c_DisplayClassa.b_7() +85 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter 过滤器,ActionExecutingContext preContext,Func1 continuation) +235491 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func1 续)+235491 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter 过滤器,ActionExecutingContext preContext,Func1 continuation) +235491 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func1 续)+235491 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList1 filters, ActionDescriptor actionDescriptor, IDictionary2 参数) +288 System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +235670 System.Web.Mvc.Controller.ExecuteCore() +174 System.Web.Mvc.MvcHandler.ProcessRequest(HttpContextBase httpContext) +209 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +599 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +171

【问题讨论】:

  • 这是一个多线程应用程序吗?

标签: nhibernate sharp-architecture


【解决方案1】:

似乎正在发生的事情是会话由于 CreateSQLQuery 调用而决定刷新。这会导致 post update 事件被添加到需要触发的内部事件列表中(猜测这个)。

您应该能够通过使用事件访问子会话而不是主 NH 会话来修复它。像这样:

public class PostUpdateListener : IPostUpdateEventListener 
{ 
    public void OnPostUpdate(PostUpdateEvent postUpdateEvent) 
    { 
        var session = postUpdateEvent.Session.GetSession(EntityMode.Poco)

        var results = session.CreateSQLQuery("Select * from Storefront").List<object>();
        for (int i = 0; i < results.Count; i++)
        {
        }
    }
}

如果您还需要在此事件中添加或更新实体,例如出于审核日志的目的,那么您还需要确保刷新内部会话。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多