xxx实例的标识符从y更改为z

时间:2010-04-12 17:45:03

标签: c# nhibernate

尝试更新db中的对象时出现以下错误。有谁知道可能会发生什么?我检查了所有数据类型,它们对应于数据库中的内容。感谢您的任何想法 -

NHibernate.HibernateException was unhandled by user code
  Message="identifier of an instance of DataTransfer.status was altered from 3 to 4"
  Source="NHibernate"
  StackTrace:
       at NHibernate.Event.Default.DefaultFlushEntityEventListener.CheckId(Object obj, IEntityPersister persister, Object id, EntityMode entityMode)
       at NHibernate.Event.Default.DefaultFlushEntityEventListener.GetValues(Object entity, EntityEntry entry, EntityMode entityMode, Boolean mightBeDirty, ISessionImplementor session)
       at NHibernate.Event.Default.DefaultFlushEntityEventListener.OnFlushEntity(FlushEntityEvent event)
       at NHibernate.Event.Default.AbstractFlushingEventListener.FlushEntities(FlushEvent event)
       at NHibernate.Event.Default.AbstractFlushingEventListener.FlushEverythingToExecutions(FlushEvent event)
       at NHibernate.Event.Default.DefaultFlushEventListener.OnFlush(FlushEvent event)
       at NHibernate.Impl.SessionImpl.Flush()
       at NHibernate.Transaction.AdoTransaction.Commit()
       at DataAccessLayer.NHibernateDataProvider.UpdateItem_temp(items_temp item_temp) in C:\Documents and Settings\Carl.PAMB\My Documents\Visual Studio 2008\Projects\InventoryDataClean\DataAccessLayer\NHibernateDataProvider.cs:line 226
       at InventoryDataClean.Controllers.ImportController.Edit(Int32 id, FormCollection formValues) in C:\Documents and Settings\Carl.PAMB\My Documents\Visual Studio 2008\Projects\InventoryDataClean\InventoryDataClean\Controllers\ImportController.cs:line 101
       at lambda_method(ExecutionScope , 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__DisplayClassa.<InvokeActionMethodWithFilters>b__7()
       at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
  InnerException: 

从我的log4net日志 -

13:37:17 [9] DEBUG NHibernate.Event.Default.DefaultSaveOrUpdateEventListener - object already associated with session: [DataTransfer.items_temp#56876]

此外,这里也称为 -

 item.status.id = Int32.Parse(formValues["Status"]);
 _provider.UpdateItem_temp(item);

以下是我的数据提供商提供的代码。

public void UpdateItem_temp(items_temp item_temp)
{
    ITransaction t = _session.BeginTransaction();
    try
    {
        _session.SaveOrUpdate(item_temp);
        t.Commit();
    }
    catch (Exception)
    {
        t.Rollback();
        throw;
    }
    finally
    {
        t.Dispose();
    }
}

1 个答案:

答案 0 :(得分:8)

这绝对是错误的:

item.status.id = Int32.Parse(formValues["Status"]);

您正在更改状态实例的ID,这是非法的。

相反,如果您尝试更改项目状态,则应该这样做:

item.status = session.Load<Status>(Int32.Parse(formValues["Status"]));

(我猜测“状态”是状态属性的类型;用正确的名称替换它)