Dapper & TransactionScope?

…衆ロ難τιáo~ 提交于 2020-01-20 13:21:46

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/6860979/dapper-transactionscope

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!