I have this code which I used for some time:
using Dapper.Contrib;
using Dapper.Contrib.Extensions;
...
async Task DBStuff()
{
OracleConnection conn = new OracleConnection();
//SqlConnection conn = new SqlConnection();
await conn.OpenAsync();
using (var tran = await conn.BeginTransactionAsync())
{
var sql = "insert stuff...";
await conn.ExecuteAsync(sql);
}
}
It works flawlessly. However when I switch the connection from OracleConnection to SqlConnection suddenly I get this error at conn.ExecuteAsync(sql): "BeginExecuteNonQuery requires the command to have a transaction when the connection assigned to the command is in a pending local transaction... "
I can get rid of error if I pass the transaction to every conn.ExecuteXXX() like this:
conn.ExecuteAsync(sql, transaction: tran)
Is there a way to make it work like with OracleConnection, i.e without having to pass the transaction every time?
According to this post (Performing an Oracle Transaction using C# and ODP.NET) Oracle doesn't need or use additional transaction settings:
The OracleCommand automatically "reuses" the transaction that is currently active on the command's OracleConnection