I just got a TooManyRowsAffectedException while working with NHibernate and I've seen workarounds for that by injecting a different batcher like here TooManyRowsAffectedException with encrypted triggers, or by modifying the triggers in the database to use SET NOCOUNT ON (I can't use this one since I don't want to modify the database -- it is very complex with more than a hundred tables all related together and I don't want to go mess with it since other applications use it). What I don't understand is why this exception happens. All I do is that I have a Sample object that's got a couple of values that I check, and if the values fit with given criteria, I set the Sample.IsDone row to 'Y' (in our database all booleans are represented by a char Y or N). The code is very simple:
IQueryable<Sample> samples = session.Query<Sample>().Where(s => s.Value == desiredValue);
foreach (Sample sample in samples)
{
sample.IsDone = 'Y';
session.Flush(); // Throws TooManyRowsAffectedException
}
session.Flush(); // Throws TooManyRowsAffectedException
The Flush call throws whether I put it inside the loop or outside. Is there something I am doing wrong or is it only related with the way the database is made? I tried calling SaveOrUpdate() on the sample before Flush(), but it didn't change anything. I know I can work around this exception, but I'd prefer understanding the source of the problem.
Note: In the exception it tells me the actual row count is 2 and the expected is 1. Why does it update 2 rows since I only change 1 row?
Thanks all for your help!
EDIT:
I was able to find out that the cause of that is because there is a trigger in the database updating a row in the Container table (Containers contain Samples) when the sample is updated. Is there a way to configure NHibernate so that it knows about this trigger and expects the right number of rows to get updated?