StaleObjectStateException我正在更新刚刚创建的用户帐户

时间:2016-08-03 20:33:28

标签: c# asp.net-mvc nhibernate locking

所以我正在处理错误:

  

行被另一个事务(或未保存的值)更新或删除   映射不正确):   [Domain.Entities.General.Shipping#0]

当用户想要在我的应用程序中更新他们的送货地址时。

控制器

var shippingToAdd = new Shipping
                {
                    UserId = newUserId,
                    FirstName = model.FirstName,
                    LastName = model.LastName,
                    Address = model.ShippingStreet,
                    SteApt = model.ShippingAptSte,
                    City = model.ShippingCity,
                    State = model.ShippingState,
                    Zip = model.ShippingZip,
                    Country = shippingCountryId,
                };
                UserManagerService.UpdateShipping(shippingToAdd);

UserManagerService

public static ISession Context { get; set; }

        public UserManagerService(ISession context)
        {
            Context = context;
        }

    public static int UpdateShipping(Shipping shipping )
    {
        using (ITransaction transaction = Context.BeginTransaction()) 
        {
            Context.Update(shipping);
            transaction.Commit();
            return 0; //Right Here is where StaleObjectStateException occurs
        }
    }

无论如何在Commit()之前解除锁定?或者最新发布锁定的最佳方式是什么?

STACK TRACE

  

[StaleObjectStateException:Row被另一个更新或删除   事务(或未保存的值映射不正确):   [Domain.Entities.General.Shipping#0]]
  NHibernate.Persister.Entity.AbstractEntityPersister.Update(Object id,   Object [] fields,Object [] oldFields,Object rowId,Boolean []   includeProperty,Int32 j,Object oldVersion,Object obj,   SqlCommandInfo sql,ISessionImplementor session)+2548
  NHibernate.Persister.Entity.AbstractEntityPersister.UpdateOrInsert(对象   id,Object [] fields,Object [] oldFields,Object rowId,Boolean []   includeProperty,Int32 j,Object oldVersion,Object obj,   SqlCommandInfo sql,ISessionImplementor session)+533
  NHibernate.Persister.Entity.AbstractEntityPersister.Update(Object id,   Object [] fields,Int32 [] dirtyFields,Boolean hasDirtyCollection,   Object [] oldFields,Object oldVersion,Object obj,Object rowId,   ISessionImplementor session)+2372
  NHibernate.Action.EntityUpdateAction.Execute()+975
  NHibernate.Engine.ActionQueue.Execute(IExecutable executable)+63
  NHibernate.Engine.ActionQueue.ExecuteActions(IList list)+165
  NHibernate.Engine.ActionQueue.ExecuteActions()+68
  NHibernate.Event.Default.AbstractFlushingEventListener.PerformExecutions(IEventSource   会议)+451
  NHibernate.Event.Default.DefaultFlushEventListener.OnFlush(FlushEvent   事件)

     

+286 NHibernate.Impl.SessionImpl.Flush()+385 Service.Account.UserManagerService.UpdateShipping(送货运送)   在   C:\用户\ WD \桌面\主\服务\帐户\ UserManagerService.cs:189
  Controllers.PayPalController.PaymentWithCreditCard(CartViewModel   model,IEnumerable 1 cookiecart) in c:\Users\wd\Desktop\master\Controllers\PayPalController.cs:552
Controllers.PayPalController.Continue(CartViewModel model) in c:\Users\wd\Desktop\master\Controllers\PayPalController.cs:136
lambda_method(Closure , ControllerBase , Object[] ) +268
System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +87
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary
2参数)+603
  System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext   controllerContext,ActionDescriptor actionDescriptor,IDictionary 2 parameters) +93
System.Web.Mvc.Async.ActionInvocation.InvokeSynchronousActionMethod() +97 System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState) +53
System.Web.Mvc.Async.WrappedAsyncResult
2.CallEndDelegate(IAsyncResult)   asyncResult)+137
  System.Web.Mvc.Async.WrappedAsyncResultBase`1.End()+ 187
  System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult,   对象标签)+136

1 个答案:

答案 0 :(得分:1)

如果您尝试添加新对象,我不明白为什么要调用Update()。你的意思是使用ISession.Save()还是ISession.SaveOrUpdate()?