1

我正在使用尖拱 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。 d:\horn.horn\orm\nhibernate\Working-2.1\src\NHibernate\Engine\ActionQueue.cs:112 NHibernate.Engine.ActionQueue.ExecuteActions() 中的 ExecuteActions(IList 列表):\horn.horn\orm \nhibernate\Working-2.1\src\NHibernate\Engine\ActionQueue.cs:147 NHibernate.Event.Default.AbstractFlushingEventListener.PerformExecutions(IEventSource 会话)在 d:\horn.horn\orm\nhibernate\Working-2.1\src\NHibernate \Event\Default\AbstractFlushingEventListener.cs:241 NHibernate.Event.Default.DefaultFlushEventListener。1.Save(T entity) in D:\Solutions\Infrastructure\NHibernate\LinqRepository.cs:95 Tasks.Shared.ContentEntityTasks4.Save(TSaveEntityRequestDetails details) 在 D:\Solutions\Tasks\Shared\ContentEntityTasks.cs:96 Web.Controllers.Entity.EntityController.Edit(EntityViewModel entityViewModel, HttpPostedFileBase fileName, HttpPostedFileBase mainImageFileName, HttpPostedFileBase thumbnailFileName) 在 D:\Solutions\ Web.Controllers\Entity\EntityController.cs:379 lambda_method(ExecutionScope, ControllerBase, Object[]) +185 System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary 2 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 filter, ActionExecutingContext preContext, Func1 continuation) +235491 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func1 续)+235491 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter 过滤器,ActionExecutingContext preContext,Func 1 continuation) +235491 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func1 续)+235491 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext,IList 1 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

4

1 回答 1

0

似乎正在发生的事情是会话由于 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++)
        {
        }
    }
}

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

于 2012-03-02T18:54:26.487 回答