How to use transactions with dapper.net?

后端 未结 5 746
夕颜
夕颜 2020-12-12 15:55

I would like to run multiple insert statements on multiple tables. I am using dapper.net. I don\'t see any way to handle transactions with dapper.net.

Please share y

5条回答
  •  独厮守ぢ
    2020-12-12 16:24

    Considering all your tables are in single database, I disagree with TransactionScope solution suggested in some answers here. Refer this answer.

    1. TransactionScope is generally used for distributed transactions; transaction spanning different databases may be on different system. This needs some configurations on operating system and SQL Server without which this will not work. This is not recommended if all your queries are against single instance of database.
      But, with single database this may be useful when you need to include the code in transaction that is not under your control. With single database, it does not need special configurations either.

    2. connection.BeginTransaction is ADO.NET syntax to implement transaction (in C#, VB.NET etc.) against single database. This does not work across multiple databases.

    So, connection.BeginTransaction() is better way to go.

    Even the better way to handle the transaction is to implement UnitOfWork as explained in this answer.

提交回复
热议问题