Jonathan Allen Jonathan Allen - 1 month ago 8
SQL Question

Can I get the underlying conneciton and transaction objects from NHibernate?

Is it possible to get the

DbConnection
and
DbTransaction
objects from a NHibernate transaction?

I need to combine some NHibernate calls with a third-part library inside the same transaction.

Answer

ISession.Connection returns the IDBConnection object. For the transaction see the blog post below.

    //http://ayende.com/blog/1583/i-hate-this-code

    private static IDbTransaction GetTransaction(ISession session)

    {
        using (var command = session.Connection.CreateCommand())
        {
            session.Transaction.Enlist(command);
            return command.Transaction;
        }
    }