SQL说,即使我创建了一个,也没有主动交易

发布于 2025-02-06 07:20:48 字数 803 浏览 2 评论 0原文

使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

じ违心 2025-02-13 07:20:49

奇怪的是,我将其跟踪到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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文