tags:

views:

120

answers:

2

What are possible reasons why NHibernate does not perform delete operation?

 public bool Delete(MyType model)
 {
     using (var session = _sessionFactory.OpenSession())
         session.Delete(model);
     return true;
 }

I tried to call session.Clear() method, that didn't help either. I'm kind a confused. :/

MyType in this case has only Id&Name. Creating operation works successfully.

+2  A: 

Flush the session, or put the Delete in a Transaction and commit the Transaction.

NHibernate will - by default - try to postpone the execution of SQL Statements as much as possible.

Frederik Gheysels
Could you provide some example code? And where to find best NHibernate documentation?
Arnis L.
A: 

This helped...

using (var session = _sessionFactory.OpenSession())
{
    using (ITransaction tx = session.BeginTransaction())
    {
        session.Delete(model);
        session.Flush();
        tx.Commit();
    }
}
Arnis L.
In this example, flushing may be unnecessary since NHibernate will flush itself when the transaction is commited. Flushing is necessary if you want to 'flush' the changes before the commit, or, if you do not have a transaction.http://nhforge.org is an interesting site regarding nhibernate.
Frederik Gheysels
Yeah... i noticed that. Switched to "session.Delete(model);session.Flush();" without transaction. Previously it didn't work cause of error in my IUserType Equals method implementation. Thanks for clearing this out. :)
Arnis L.