与Dapper,Oracle vs MS中的C#交易
我有一个我使用了一段时间的代码:
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);
}
}
它可完美地工作。但是,当我将连接从oracleconnection切换到SQLConnection时,我会在Conn.executeasync(SQL)中获得此错误: “ BegineXecutenOnquery需要命令在分配给命令的连接处于待处理的本地交易中时必须进行交易
”
…… (SQL,Transaction:Tran)
是否有办法使其像OracleConnection一样,即不必每次通过交易?
根据这篇文章(使用C#和C#和ODP.NET )Oracle不需要或使用其他交易设置:
oraclecommand自动“重用”交易是 当前在命令的OracleConnection上活动
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最后,我决定放下Dapper Transactions并使用TransactionsCope。
我正在写这篇文章,所以也许会帮助某人,并纠正我对此主题发现的一些评论。该代码看起来像这样:
请注意,要使用异步方法,必须使用transactionscopeasyncflowoption.mabled选项。
该代码比Dapper代码具有多个优点:
In the end I decided to drop Dapper transactions and use TransactionScope.
I'm writing this so maybe will help someone and to correct some comments that I found about this subject. The code will look like this:
Note that in order to work with Async methods TransactionScopeAsyncFlowOption.Enabled option must be used.
The code has several advantages over Dapper code: