34

Getting this every time I attempt to CREATE a particular entity ... just want to know how I should go about figuring out the cause.

I'm using Fluent NHibernate auto-mapping so perhaps I haven't set a convention appropriately and/or need to override somethings in one or more mapping files. I've gone thru a number of posts on the web regarding this problem and having a hard time figuring out exactly why it is happening in my case.

The object I'm saving is pretty simple. It is a "Person" object that references a "Company" entity and has a collection of "Address" entities. UPDATES work fine on existing Person objects that are already in the database.

Any suggestions?

Amit Joshi
  • 15,448
  • 21
  • 77
  • 141
wgpubs
  • 8,131
  • 15
  • 62
  • 109

5 Answers5

29

The error means that the SQL INSERT statement is being executed, but the ROWCOUNT being returned by SQL Server after it runs is 0, not 1 as expected.

There are several causes, from incorrect mappings, to UPDATE/INSERT triggers that have rowcount turned off.

Your best beat is to profile the SQL statements and see what happens. To do that either turn on nHibernate sql logging, or use the sql profiler. Once you have the SQL you may know the cause, if not try running the SQL manually and see what happens.

Also I suggest you to post your mapping, as it will help people spot any issues.

Community
  • 1
  • 1
Iain
  • 10,814
  • 3
  • 36
  • 31
  • 1
    Yup. In my case I'm using the SharpArchitecture framework which defaults unsaved values for "Id" field to zero ... instead of -1 as I've been accustomed to using for years. I tried overriding this in my automapping config and even in a custom mapping class ... but still sets it to 0. – wgpubs Nov 03 '10 at 22:03
  • 1
    So you tried this: Id(a => a.Id).UnsavedValue(0); If you are still having issues and want me to have a look, add the resulting SQL and your mapping into your question. – Iain Nov 03 '10 at 22:57
  • 1
    i had this problem because a null object was being added to a collection then a save was being done. – m4tt1mus Oct 09 '14 at 22:54
16

This can happen when trigger(s) execute additional DML (data modification) queries which affect the row counts. My solution was to add the following at the top of my trigger:

SET NOCOUNT ON;
Mr. TA
  • 5,230
  • 1
  • 28
  • 35
2

This may occur because of Auto increment primary key. To solve this problem do not inset auto increment value with data set. Insert data without the primary key.

MadukaJ
  • 722
  • 6
  • 22
0

When targeting a view with an INSTEAD OF trigger it can be next to impossible to get the correct row count. After delving a bit into the source I found out that you can make a custom persister which makes NHibernate ignore the count checks.

public class SingleTableNoResultCheckEntityPersister : SingleTableEntityPersister
{
    public SingleTableNoResultCheckEntityPersister(PersistentClass persistentClass, ICacheConcurrencyStrategy cache, ISessionFactoryImplementor factory, IMapping mapping)
        : base(persistentClass, cache, factory, mapping)
    {
        for (int i = 0; i < this.insertResultCheckStyles.Length; i++)
        {
            this.insertResultCheckStyles[i] = ExecuteUpdateResultCheckStyle.None;
        }

        for (int i = 0; i < this.updateResultCheckStyles.Length; i++)
        {
            this.updateResultCheckStyles[i] = ExecuteUpdateResultCheckStyle.None;
        }

        for (int i = 0; i < this.deleteResultCheckStyles.Length; i++)
        {
            this.deleteResultCheckStyles[i] = ExecuteUpdateResultCheckStyle.None;
        }
    }
}
Robin Palm
  • 111
  • 6
0

if you get row count greater than 1 its usually because you have duplicate rows in a table that have same ids. Check your table for duplicated records. Deleting duplicates will fix it.

NHibernate.AdoNet.TooManyRowsAffectedException: 'Batch update returned unexpected row count from update; actual row count: 2; expected: 1'

ozanmut
  • 2,898
  • 26
  • 22