简体   繁体   English

批更新从更新返回意外行数; 实际行数:0; 预期:使用NHibernate进行删除操作时为1

[英]Batch update returned unexpected row count from update; actual row count: 0; expected: 1 when delete operation using NHibernate

I use NHibernate in c# and perform delete operation: My problem is that, When I login by 'A' user on 'PC1' and also login by same user on 'PC2' because I provide no. 我在c#中使用NHibernate并执行删除操作:我的问题是,当我由“ PC1”上的“ A”用户登录并且也由“ PC2”上的同一用户登录时,因为我没有提供。 of session 2 for this user and delete one 此用户的会话2中,删除一个

record from 'PC1' record deleted successfully, But when same record delete from 'PC2' without refreshing page.then it gives exception as follows: 从'PC1'中的记录删除成功,但是当从'PC2'中删除相同记录而不刷新页面时,则出现如下异常:

"Batch update returned unexpected row count from update; actual row count: 0; expected: 1" “批量更新从更新返回了意外的行计数;实际的行计数:0;预期的:1”

here my code is : 这是我的代码:

public void Delete(object instance)
        {
            using(NHSession.BeginTransaction())
            {
                NHSession.Delete(instance);
                NHSession.Transaction.Commit();
            }
        }

here - 这里 -

NHSession.Transaction.Commit(); NHSession.Transaction.Commit(); this line gives error when I delete from 'PC2'. 当我从“ PC2”中删除时,此行显示错误。 so here how to handle this problem using NHibernate. 因此,这里介绍如何使用NHibernate处理此问题。 After executing this make sql query as follows: 执行此make sql查询后,如下所示:

DELETE FROM [Employee].[dbo].Employee_Master WHERE EmpID = @p0;@p0 = d3b9df34-97c6-450a-a570-a62000dd5125 [Type: Guid (0)]

and when same query execute in sql Studio then it return following o/p : 当在sql Studio中执行相同的查询时,它将返回以下o / p

(0 row(s) affected) (0行受影响)

It's caused by a weird way of coping with transactions or detached entities. 这是由处理事务或分离实体的怪异方式引起的。 When you avoid deleting detached entities directly, it doesn't happen. 如果您避免直接删除分离的实体,则不会发生。

I don't know why your transaction looks like this. 我不知道您的交易为什么会这样。

Either you should have started the transaction from some higher layer (never manage transactions from within the data access layer), or you get the object from the client directly and should load it as attached instance. 您要么应该从更高的层启动事务(从不从数据访问层管理事务),要么直接从客户端获取对象,然后将其作为附加实例加载。 In that case, you should only get the id from the client instead of the detached instance: 在这种情况下,您应该仅从客户端而不是分离的实例获取ID:

public void DeleteUserSession(Guid id)
{
    using(NHSession.BeginTransaction())
    {
      var instance = NHSession.Get(typeof(UserSession), id);
      if (instance != null)
      {
        NHSession.Delete(instance);
      }
      NHSession.Transaction.Commit();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM