如果 Commit 失败了,会抛出异常并回滚事务。所以需要在 try-catch 块中包含 Commit 方法,以便在失败时正确处理异常并回滚事务。

using(var trans = dbContext.Database.BeginTransaction())
{
  try
  {
    Invoke();
    trans.Commit();
  }
  catch (Exception ex)
  {
    trans.Rollback();
    // 处理异常
  }
}
C# 中使用事务时,Commit 失败会自动回滚吗?

原文地址: https://www.cveoy.top/t/topic/m8Jm 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录