当同时存在与数据库的多个连接时,InvalidOperationException

时间:2019-09-13 03:48:28

标签: .net

我正在使用此功能来选择SQL Server数据库中的项目。当只有一个数据库连接时,它可以按预期工作。但是,当同时有多个数据库连接时,将导致System.InvalidOperationException。

另一个问题是它没有固定的错误消息,当我多次尝试时,它随机地给了我这3个消息之一:

  

“消息”:“发生错误。”,     “ ExceptionMessage”:“连接当前已登记事务。完成当前事务并重试。”,     “ ExceptionType”:“ System.InvalidOperationException”

     

“消息”:“发生错误。”,     “ ExceptionMessage”:“无效的操作。连接已关闭。”,     “ ExceptionType”:“ System.InvalidOperationException”

     

“消息”:“发生错误。”,     “ ExceptionMessage”:“连接未关闭。连接的当前状态为正在连接。”,     “ ExceptionType”:“ System.InvalidOperationException”

这里是所说的功能,你知道可能是什么问题吗?

Public Function SelectTable(Of T As {Class, New})(Optional ByVal Where As Func(Of T, Boolean) = Nothing) As List(Of T)

        Dim AllRows As List(Of T) = Nothing
        Using dc As New DataContext(_SQLCon)

            dc.ObjectTrackingEnabled = False
            Using fc As New System.Transactions.TransactionScope(_ScopeOption, New System.Transactions.TransactionOptions With {.IsolationLevel = _OtherIsoLevel})

                dc.Transaction = _Transac
                dc.CommandTimeout = _TimeOut
                If Where Is Nothing Then
                    AllRows = dc.GetTable(Of T).ToList()
                Else
                    AllRows = dc.GetTable(Of T).Where(Where).ToList()
                End If
                fc.Complete()
            End Using
            dc.Dispose()
        End Using

        Return AllRows
    End Function

应该为每个请求创建一个新的DataContext,所以我不确定DataContext是否是问题所在。

我环顾四周,发现TransactionScope可能是问题所在,但是即使我从代码中取出它,它仍然无法正常工作。

先谢谢您。

0 个答案:

没有答案