I have meeting a problem, the code look like simple, but exception:
DDS.Model.ATest atest = new DDS.Model.ATest();
atest.AID = Guid.NewGuid();
ISession session = SessionProvider.GetNewSession();
using (ITransaction transaction = session.BeginTransaction())
{
session.SaveOrUpdate(atest);
int count = session.CreateQuery("from ATest").List().Count;
//Above row throw a exception:
//Batch update returned unexpected row count from update; actual row count: 0; expected: 1
transaction.Commit();
}
You are trying to load items before saving. Commit the transaction first, and then execute the query.
But that doesn’t seem to be the problem in your case. I believe you have ID mapped as Guid or Guid.comb. You should not assign the value to ID. NHibernate will take care of that.
When you assign the value and call session.SaveOrUpdate(), it will try to do update since ID value is not
Guid.Empty. The update method will fail with the exception: Batch update returned unexpected row count from update; actual row count: 0; expected: 1, sinceUPDATE ... WHERE AID = <some guid>will be executed.