I Hate This Code

time to read 1 min | 170 words

I'm trying to run a SqlBulkCopy operation as a part of NHibernate transaction, and it is not going so well. It wants to either start its own transaction, or join an existing one. The problem is that I don't have access to the real transaction in NHibernate, it is wrapped away.

Here is how I finally solved the issue:

 

private SqlTransaction GetTransaction(ISession session)

{

    using(IDbCommand command = session.Connection.CreateCommand())

    {

        session.Transaction.Enlist(command);

        return command.Transaction as SqlTransaction;

    }

}

 

Ugly, but it seems to be working.