Transactions in “dapper-dot-net”

蹲街弑〆低调 提交于 2019-12-05 10:59:31

I haven't run into any limitations with using sprocs and the risks you have with dapper are the same risks you would have with sprocs

Here is a simple example on how to use transactions with dapper

using (var connection = Db.GetConnection())
{
     connection.Open();
     IDbTransaction transaction = connection.BeginTransaction();
     try
     {
         var newId= connection.Query<int>(@"Select id from table1 where id=@id", new{id}, transaction).Single();
         connection.Execute(@"INSERT into table1 ...",new {p1, p2}, transaction);
         connection.Execute(@"INSERT into table2 ...",new {p1, p2}, transaction);
         transaction.Commit();
     }
     catch (Exception ex)
     {
         transaction.Rollback();
     }
}
Marc Gravell

dapper can use both ado.net transactions and implicit transactions. For ado.net transactions, just create the transaction normally and provide it via the transaction parameter that is available on the main methods. For implicit transactions, just use TransactionScope normally.

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