How to use transactions with dapper.net?

后端 未结 5 755
夕颜
夕颜 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条回答
  •  猫巷女王i
    2020-12-12 16:27

    I preferred to use a more intuitive approach by getting the transaction directly from the connection:

    // This called method will get a connection, and open it if it's not yet open.
    using (var connection = GetOpenConnection())
    using (var transaction = connection.BeginTransaction())
    {
        connection.Execute(
            "INSERT INTO data(Foo, Bar) values (@Foo, @Bar);", listOf5000Items, transaction);
        transaction.Commit();
    }
    

提交回复
热议问题