You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET 7升级后调用BeginTransactionAsync抛出事务异常问题咨询

问题原因与解决方案

可能的原因

  1. EF Core 连接管理逻辑变更:.NET 7 中 EF Core 优化了数据库连接的生命周期管理,默认不会长期维持连接打开状态。调用 BeginTransactionAsync 时,如果连接处于关闭状态,EF Core 会尝试自动打开连接并创建事务,但如果存在连接被意外释放、状态同步不一致的情况,就会抛出该异常。
  2. 提前缓存 DbConnection 导致状态不一致:你的代码在构造函数中提前获取并缓存了 _connection = _db.Database.GetDbConnection(),但 EF Core 内部可能会根据操作需求切换或重置连接状态,这个缓存的实例无法同步最新的连接状态,进而干扰事务创建流程。

修复方案

方案1:移除提前缓存的 DbConnection,依赖 EF Core 自动管理

直接删除类中 _connection 私有字段,不需要手动缓存连接实例,EF Core 会自动处理连接的打开、关闭与状态同步:

public class DBservice<TDB> where TDB : DbContext
{
   private readonly IServiceProvider _services;
   private readonly DbContext _db;
   private readonly IHttpContextAccessor _context;

   private IDbContextTransaction _transaction { get; set; }
   
   public DBservice(IServiceProvider services, IHttpContextAccessor context)
   {
      _services = services;
      _db = services.GetRequiredService<TDB>();
      _context = context;
   }

   public async Task BeginTransaction()
   {
      if (_transaction is not null) { return; }
      _transaction = await _db.Database.BeginTransactionAsync(IsolationLevel.ReadUncommitted);
   }
}

方案2:创建事务前显式确保连接已打开

如果需要手动控制连接状态,可以在创建事务前检查并打开连接:

public async Task BeginTransaction()
{
   if (_transaction is not null) { return; }
   var dbConnection = _db.Database.GetDbConnection();
   if (dbConnection.State != ConnectionState.Open)
   {
      await _db.Database.OpenConnectionAsync();
   }
   _transaction = await _db.Database.BeginTransactionAsync(IsolationLevel.ReadUncommitted);
}

方案3:采用 EF Core 推荐的事务生命周期管理方式

为避免事务实例长期持有引发的资源泄漏或状态问题,建议使用 using 块管理事务,确保事务被正确释放:

public async Task ExecuteInTransaction(Func<Task> operation)
{
   using var transaction = await _db.Database.BeginTransactionAsync(IsolationLevel.ReadUncommitted);
   try
   {
      await operation();
      await transaction.CommitAsync();
   }
   catch
   {
      await transaction.RollbackAsync();
      throw;
   }
}

额外注意事项

  • 检查 DbContext 的依赖注入生命周期:确保 DBservice 和 DbContext 的生命周期匹配(比如同为 Scoped),避免 DbContext 被提前回收导致连接失效。
  • 升级 NuGet 包时确保所有 EF Core 相关包版本完全一致,避免版本不兼容引发的异常。

内容的提问来源于stack exchange,提问作者PetrK

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 03:40:33