2兄弟嵌套的transactionScope给出:事务已中止

Omu*_*Omu 7 .net c# ado.net transactionscope

此代码给出了错误:事务已中止. 如果我删除1个嵌套事务而不是它不抛出

  using(var scope = new TransactionScope())
    {
        repo.Insert(new Foo {Fname = "aaaa"});
        using(var s = new TransactionScope())
        {
            repo.Insert(new Foo { Fname = "aaaa" });

            //if I remove this transaction it is not going to throw exception
            using (var aaa = new TransactionScope())
            {
                repo.Insert(new Foo { Fname = "aaaa" });
            }

            using(var ssa = new TransactionScope())
            {
                repo.Insert(new Foo { Fname = "aaaa" });
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

Vla*_*adV 12

什么语句会引发错误?我认为这是最后一次repo.Insert.

由于您不调用scope.Complete(),因此在放置aaa时会回滚(中止)事务.
通常,事务回滚被视为错误,因此所有更高级别的事务也变得不可提交(或立即回滚).
因此,对于最后一个repo.Insert没有有效的事务要使用 - 这就是它抛出异常的原因.