nate的TooManyRowsAffectedExceptio

nate的TooManyRowsAffectedExceptio

本文介绍了NHibernate的TooManyRowsAffectedException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将更改刷新到db时出现此错误.我只更改Matricula属性.我曾经尝试刷新更改,以确保没有任何未决的事情,并且确实没有任何作用.

I'm getting this error while flushing changes to db. I am changing only the Matricula property. I have tried to flush changes before to make sure isn't there anything pending and in deed it does nothing.

接下来我要做的就是在刷新更改后立即更改该属性. 生成的SQL是正确的,但是它说应该只应有1条记录才影响212行.

Next thing I do is change that property and inmediately after flush changes. The SQL generated is correct, however it says that 212 rows where affected when only 1 should have been.

[16:49:27,122] NHibernate.SQL - DEBUG: Batch commands:
command 0:UPDATE Ordenes_Bonos SET Matricula = @p0 WHERE IdOrden = @p1 AND
IdBono = @p2;@p0 = '020012' [Type: String (4000:0:0)], @p1 = 115862 [Type:
Int32 (0:0:0)], @p2 = 10 [Type: Int32 (0:0:0)]

[16:49:27,566] NHibernate.Event.Default.AbstractFlushingEventListener -
ERROR: Could not synchronize database state with session
NHibernate.AdoNet.TooManyRowsAffectedException: Batch update returned
unexpected row count from update; actual row count: 212; expected: 1

因此,接下来我要做的是直接在数据库上检查同一查询,以防万一由于触发器而改变了很多行(我不这么认为,但以防万一):

So the next thing I do is check the same query directly on the database in case there were so many rows altered due to triggers (I don't think so but just in case):

BEGIN TRANSACTION;
UPDATE Ordenes_Bonos
SET    Matricula = '020012'
WHERE  IdOrden = '115862'
   AND IdBono = '10'

SELECT @@ROWCOUNT;
ROLLBACK;

其输出为1.

那么NHibernate为什么说在该UPDATE上更改了212行?

So why is NHibernate saying 212 rows changed over that UPDATE?

推荐答案

您的问题是由于您所说的触发器从执行语句中启动而引起的.

Your problem is caused because of the triggers you said get kicked off from the executing statement.

触发器在正在执行的语句的事务中运行,该语句是您的NHibernate更新.如果您尚未明确开始交易,则Nhibernate将使用 SQL中的隐式事务.

Triggers run within the transaction of the executing statement which is your NHibernate update. If you haven't explicitly started a transaction, Nhibernate will use an implicit transaction in SQL.

因此Nhiberate将这些触发器更新视为其事务的一部分,并引发了错误.

So Nhiberate is seeing those trigger updates as part of its transaction and is throwing your error.

所以您有2个选择.

  1. 使用set NOCOUNT ON更新所有触发器以抑制受影响的行数

  1. update all the triggers with set NOCOUNT ON to suppress the number of rows affected

删除了触发器,并将其移至NHibernate的EventListener中.

removed the triggers and move them into EventListeners in NHibernate.

这篇关于NHibernate的TooManyRowsAffectedException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 00:35