SQL说,即使我创建了一个,也没有主动交易
使用Microsoft.EntityFrameWorkCore版本6.0.5,带有.NET 6,我正在进行交易中的多个数据库操作。当我调用commitasync
时,它失败了:
没有主动交易时无法发出保存交易。
当我明确创建一次时,如何没有主动交易?
var task = await Context.Database.BeginTransactionAsync(cancellationToken);
try {
var newEntity = ...
... Multiple database operations ...
await Context.SaveChangesAsync(cancellationToken);
... Other DB operations that now make use of the just created objects.
await Context.SaveChangesAsync(cancellationToken);
await task.CommitAsync(cancellationToken);
return newEntity;
} catch (Exception) {
await task.RollbackAsync(cancellationToken);
throw;
}
我正在在不同表中创建多个对象,这些对象互相引用,并调用其他人的代码,这些代码可能正在自行保存操作,从而进行了交易。
Using Microsoft.EntityFrameworkCore version 6.0.5, with .NET 6, I'm doing multiple database operations inside of a transaction. When I call CommitAsync
, it fails saying:
Cannot issue SAVE TRANSACTION when there is no active transaction.
How is there no active transaction when I've clearly created one?
var task = await Context.Database.BeginTransactionAsync(cancellationToken);
try {
var newEntity = ...
... Multiple database operations ...
await Context.SaveChangesAsync(cancellationToken);
... Other DB operations that now make use of the just created objects.
await Context.SaveChangesAsync(cancellationToken);
await task.CommitAsync(cancellationToken);
return newEntity;
} catch (Exception) {
await task.RollbackAsync(cancellationToken);
throw;
}
I'm creating multiple object in different tables that reference each other, and calling other people's code that might be doing a save operation of its own, thus the transaction.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
奇怪的是,我将其跟踪到SQL Server损坏。我重命名了一个表,由于某种原因,如果我保存了该表的更改,则它是内部(在SQL Server中)试图使用该旧名称的。我把桌子加了重新创建,然后我没有更多的交易问题。
Strangely I tracked this down to SQL server corruption. I had renamed a table and for some reason, if I saved changes to that table, it was internally (in the SQL server) trying to use that old name. I nuked the table and recreated and then I had no more transaction issues.