I am trying to use a transaction scope inside a loop. The entire loop takes place using a single connection to the database. I am using entity framework 4 for database access. During the second iteration of the loop, when the LINQ to Entites query executes, an exception is thrown stating that the MSDTC on the server is unavailable.
I've read that explicitly opening the connection and then enlisting the transaction is supposed to solve this problem, but it has not. Below is sample code that mirrors the basic operation that is taking place.
Any ideas on how to prevent an escalation to MSDTC?
Using context = New MyEntities()
Dim connection = context.Connection
connection.Open()
For index = 0 to (Me.files.Count - 1)
Dim query = From d In context.Documents
Where (d.DocumentID = documentID)
Select d.Status
Dim status = query.FirstOrDefault()
Using trans = New TransactionScope()
connection.EnlistTransaction(Transaction.Current)
Dim result = context.UpdateStatus(True)
If (result = 1) Then
WriteToFile()
trans.Complete()
End If
End Using
Next
End Using
Edit: Instead of TransactionScope, if I use connection.BeginTransaction(), transaction.Commit(), and transaction.Rollback(), it works fine. However, I would still like to find a way to make TransactionScope work.