Dapper & TransactionScope?

coding4fun picture coding4fun · Jul 28, 2011 · Viewed 21.7k times · Source

I just started playing around with Dapper. So far i love it. Does dapper not work with TransactionScope? I noticed that even if i never call TransactionScope.Complete then my changes are still committed to the database. If TransactionScope isn't supported now is there any plans in the future to support it? If not then you have to use traditional transaction management (System.Transactions.Transaction)?

Update: I just talked to Sam over Twitter. It should work. I'll update it tomorrow morning (at work) with the details to see if anyone can figure out why my changes were still being committed to the db even when i never called complete.

Answer

coding4fun picture coding4fun · Jul 29, 2011

It was totally my fault and not fully understanding transactionscope. A connection is not automatically enlisted in transactionscope unless you open the connection within the transactionscope:

Automatic Enlistment

  using (var scope = new TransactionScope())
      {
        con.Open();                                
         //update/delete/insert commands here
      }

Manual Enlistment

    con.Open();
    using (var scope = new TransactionScope())
    {
       con.EnlistTransaction(Transaction.Current);  
       //update/delte/insert statements here
    }

Details can be found here: Details