C# – Nhibernate unexpected row count rising after deletes

cnhibernaterowcount

Problem:

I'm getting error:

Batch update returned unexpected row count from update; actual row
count: 0; expected: 1

when some entity is deleted between fetch and delete. After any next delete row count and expected are rising for e.g.:

Batch update returned unexpected row count from update; actual row
count: 11; expected: 12

Code:

var queueEntry = _session.Query<MyTable>().FirstOrDefault(x => x.Id == id);
if (queueEntry != null)
{
    _session.Delete(queueEntry);

    try
    {
        _session.Flush();
    }
    catch (Exception ex)
    {
        _session.Clear();
    }

    return 1;
}

Attempts:

I've tried few things with no success, I've added this lines in catch clause:

_session.Disconnect();
_session.Reconnect();

I've tried to wrap whole code I've putted here with transaction and instead of _session.Flush() I putted transaction.Commit().

Mappings:

This is mapping by code

public class MyTable : IEntity
{
    public virtual int Id { get; protected set; }
    public virtual string Type { get; set; }
    public virtual string Source { get; set; }
    public virtual string OperationType { get; set; }
    public virtual bool IsVisible { get; set; }
    public virtual DateTime? Timestamp { get; set; }
    public virtual string ContentId { get; set; }
    public virtual int Priority { get; set; }
}

Question:

Is there any way to reset this expected value, so I can continue removing values without exceptions?

Of course entities are removed from table, but I keep getting exceptions. Using empty catch clause for ignoring this exceptions is not a solution for me.

EDIT:

Addtional info: this table is queue for long running process, it has no child tables, just few things usefull for service. Posted mappings. It happens after some time, not after first delete.

Best Answer

Usually this error means that the SQL statement is successfully executed, but the ROWCOUNT returned by SQL Server is 0, not 1 as expected.

There can be several causes for this, like incorrect mappings, triggers that have rowcount turned off.

You will be able to get clear understanding of the issue by checking the SQL statements execute in the sql server. It can be done by either turnning on nHibernate sql logging, or using 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 if you can post the mapping, it will help people spot any issues

Related Topic