当程序试图访问水果数据库时,我遇到了一个挂起。我已经在我的开发计算机和SQL Server服务器上启用了网络访问MSDTC。
代码:
(请原谅代码着色……我对vb.net的误解也是如此)
Using ts As New TransactionScope
Dim fruit As New FruitDataContext
Dim thingies As New ThingiesDataContext
If (From f In fruit.tblApples Where f.Rotten = "Yes" AndAlso f.batch = 1).Count >= 1 Then
'Record today's date as the day that the rotten apples were dumped.
End If
'Other complicated code that uses ThingiesDataContext and FruitDataContext
du.SubmitChanges()
ts.Complete()
End Using
编辑:
我又多挖了一点,结果发现问题出在LINQ的生产线上。当我尝试使用Linq to SQL可视化工具查看它时,我得到以下错误:
System.InvalidCastException: Specified cast is not valid.
at LinqToSqlQueryVisualizer.SqlQueryInfo.deserialize(Stream stream)
at LinqToSqlQueryVisualizer.Visualizer.Display(IDialogVisualizerService windowService, Stream rawStream)
at LinqToSqlQueryVisualizer.DialogChooser.Show(IDialogVisualizerService windowService, IVisualizerObjectProvider objectProvider)
at Microsoft.VisualStudio.DebuggerVisualizers.DebugViewerShim.ManagedShim.DelegatedHost.CreateViewer(IntPtr hwnd, HostServicesHelper hsh, SafeProxyWrapper proxy)
我还编辑了Linq语句,使其更接近我的实际代码。
最终编辑:
我尝试使用普通的sqlconnection而不是“thingies as new thingiesdataContext”,但问题仍然存在。
TransactionScope似乎无法处理同一事务中的多个SQL连接。
微软官方说明
SQL Server不支持并行事务。
来自MSDN:
http://msdn.microsoft.com/en-us/library/bb896149.aspx