How to solve “Batch update returned unexpected row count from update; actual row count: 0; expected: 1” problem?

房东的猫 提交于 2019-11-30 16:54:31
Iain

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.

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;

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.

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;
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!