NHibernate.StaleStateException: Batch update returned unexpected row count from update; actual row count: -1; expected: 1

Can you check if you have NOCOUNT on or off on the SQL server instance? NHibernate requires this to be OFF so that it gets the row count returned.

If this is not possible then you can customize the connection factory used by NHibernate to disable this. The code for this can be found at the following issue:

Code:

public class CustomDriverConnectionProvider : NHibernate.Connection.DriverConnectionProvider
{
    public override System.Data.IDbConnection GetConnection()
    {
        var conn = base.GetConnection();
        DisableNoCount(conn);
        return conn;
    }

    private void DisableNoCount(System.Data.IDbConnection connection)
    {
        using (var command = connection.CreateCommand())
        {
            command.CommandText = "SET NOCOUNT OFF";
            command.ExecuteNonQuery();
        }
    }
}

Configuration:

<appSettings>
  <add
      key="NServiceBus/Persistence/NHibernate/connection.provider"
      value="CustomDriverConnectionProvider, MyAssembly" />
</appSettings>

I highly recommend disabling this on the instance level to avoid any confusion between environments.

1 Like