C# 如何使用多个嵌套的TransactionScope?

C# 如何使用多个嵌套的TransactionScope?,c#,linq-to-sql,transactions,C#,Linq To Sql,Transactions,我正在linq2sql中执行一些需要在事务中运行的操作。但是,我在事务内部使用的一些方法也使用linq2sql并在自己的事务内部运行(内部事务在存储过程中运行)。这给了我一个例外 [TransactionInDoubtException: The transaction is in doubt.] System.Transactions.TransactionStateInDoubt.EndCommit(InternalTransaction tx) +76 with the inner

我正在linq2sql中执行一些需要在事务中运行的操作。但是,我在事务内部使用的一些方法也使用linq2sql并在自己的事务内部运行(内部事务在存储过程中运行)。这给了我一个例外

[TransactionInDoubtException: The transaction is in doubt.]
   System.Transactions.TransactionStateInDoubt.EndCommit(InternalTransaction tx) +76
with the inner exception
[SqlException (0x80131904): There is already an open DataReader associated with this Command which must be closed first.]
[SqlException (0x80131904): The transaction operation cannot be performed because there are pending requests working on this transaction.]
如果对SQL Server使用MultipleActiveResultSets,则会得到异常

[TransactionInDoubtException: The transaction is in doubt.]
   System.Transactions.TransactionStateInDoubt.EndCommit(InternalTransaction tx) +76
with the inner exception
[SqlException (0x80131904): There is already an open DataReader associated with this Command which must be closed first.]
[SqlException (0x80131904): The transaction operation cannot be performed because there are pending requests working on this transaction.]

有没有人有过以这种方式使用linq2sql和TransactionScope的经验?

我知道我们在我目前正在处理的项目中遇到过这个问题,我知道这与LinqToSql在使用存储过程时以错误的方式生成dbml文件有关。我们必须手动操作才能让它工作。LinqToSql显然从存储过程返回了ISingeResult,并生成了错误

我不是纠正错误的人,但我知道这与此有关

更多信息:

这对我来说是一个“掌心”时刻,但考虑到我看到了这种确切的行为,并且它没有立即击中我,我想我应该继续并将其作为一种可能性发布:

当我将TransactionScope设置为ReadUncommitted时,我看到了这种行为:

using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, 
       new TransactionOptions { IsolationLevel = IsolationLevel.ReadUncommitted })

我的LINQtoSQL调用了一个存储过程来返回过程的结果。face palm是指在proc本身中可以指定WITH(NOLOCK)SQL提示,因此不需要将Linq to SQL查询包装在ReadUncommitted事务范围中。(至少在我的情况下)

我想,您正在尝试读取一些数据并动态修改它(而读取仍在进行)。

在这种情况下,最简单的选择是先读取所有数据(例如,使用IEnumerable.ToList()扩展方法),然后对数据执行所有操作。

我知道这是一个旧线程,但这是谷歌在这个错误上的最高成功率,所以我认为值得添加我的答案。在我的例子中,我使用的是实体框架,所以甚至不确定它是否适用,但即使如此,一些使用EF的人可能会来这里。在我的例子中,这是因为我调用了一个存储过程,没有对返回的行执行任何操作。因为EF没有读回行,所以这是“打开操作”。我的解决方案是简单地将First()添加到每个过程调用的末尾:

myentities.CallMyStoredProc(A, B, C).First();

这是我的问题,上面有一个类似的问题,所以我+1:)