【问题标题】:Re-use transaction when saveChanges() fail + EFsaveChanges() 失败时重用事务 + EF
【发布时间】:2020-05-19 10:59:18
【问题描述】:

如果 context.savechanges() 失败,事务数据库状态和事务状态会是什么。我可以重复使用相同的事务但创建一个新的上下文吗?

// Should the retry be at this level in case saveChanges() fail.
    using(var transaction = new TransactionScope())
    {
    // retry at this level in case saveChanges() fail. Use the ambient transaction?
      using(var context = new DbContext)
      {
      //do some update and encounter exception (e.g., concurrency exception.)
      context.saveChanges();
      }
    transaction.Complete();
    }

【问题讨论】:

    标签: c# entity-framework transactionscope savechanges


    【解决方案1】:

    如果 context.savechanges() 失败,事务数据库状态和事务状态会是什么。我可以重复使用相同的事务但创建一个新的上下文吗?

    确切的行为是特定于提供者和特定于错误的。但一般的答案是失败可能会破坏整个事务,甚至是 DbContext,因此您无法在事务中的任意失败后重试。

    它可能适用于并发异常或其他特定故障,但您需要将它们作为特殊情况进行测试和处理。

    【讨论】:

      猜你喜欢
      • 2023-03-13
      • 1970-01-01
      • 2012-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多