THE SQL Server Blog Spot on the Web

Welcome to SQLblog.com - The SQL Server blog spot on the web Sign in | Join | Help
in Search

Michael Zilberstein

NHibernate and triggers

Recently had an interesting experience with NHibernate (I was against working with it from the beginning for many reasons, but unfortunately I wasn't the one to decide). I added trigger to a table that performs several update / insert operation to other tables. Suddenly updating table via NHibernate returned error:

NHibernate.AdoNet.TooManyRowsAffectedException: Unexpected row count: 6; expected: 1

So NHibernate doesn't support triggers that perform DML operations? Not quite. The solution is: add SET NOCOUNT ON before trigger performs DML operations. Looks like NHibernate doesn't really count DML operations, but messages of the type "2 rows updated" returned by SQL Server. And NOCOUNT setting suppresses these messages.


Published Wednesday, November 19, 2008 12:24 PM by Michael Zilberstein

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

AaronBertrand said:

Probably a good idea in all triggers, whether you use NHibernate or not, unless you have a really good reason NOT to suppress the rowcount from each statement.  I add this directive to all triggers and stored procedures as a simple matter of course, and have done so for several years.

November 20, 2008 8:40 PM
 

Reflective Perspective - Chris Alcock » The Morning Brew #229 said:

November 24, 2008 5:15 AM
 

Gauthier Segay said:

As far as I know, it does the same as checking ado.net's ExecuteNonQuery return value.

I think checking this value is a good practice in plain old ado.net as well while performing any side effect operation.

April 26, 2009 5:54 AM

Leave a Comment

(required) 
(optional)
(required) 
Submit
Powered by Community Server (Commercial Edition), by Telligent Systems
  Privacy Statement