最初に EF コードを使用していますが、TransactionScope のコンテキストでクエリを実行できないようです。まさにこれを行う例を何十も見てきましたが、挿入はトランザクションされていません。このテストは失敗し、レコードは DB に残っていますが、エラーはスローされません。
私は SQL 2008 を使用しており、MSDTC がオンになっていますが、この環境では分散トランザクションにエスカレートされるべきではないため、オンにする必要はないと理解しています。いずれにせよ、エスカレートしようとして失敗した場合、例外が発生すると思います。
<TestMethod()>
Public Sub CanEnrollInTransaction()
Dim id = Guid.NewGuid().ToString()
Using foo = New TransactionScope(
TransactionScopeOption.Required,
New TransactionOptions() With {.IsolationLevel = IsolationLevel.Serializable})
Dim context = New WebPersistentDataContext() 'Subclass of DbContext
context.WebsiteErrors.Add(New WebsiteError() With {
.WebsiteId = 21,
.AdditionalInformation = id,
.BrowserInformation = "Unit test",
.ErrorDateTime = DateTime.Now,
.ErrorMessage = "Unit Test"})
'No transaction in SQL Profiler
context.SaveChanges()
'Tried this too...
'Dim objectContext = CType(context, IObjectContextAdapter).ObjectContext
'objectContext.SaveChanges()
'Not committing should roll back the transaction when scope is disposed
End Using
Dim newContext = New WebPersistentDataContext()
If newContext.WebsiteErrors.Any(Function(e) e.AdditionalInformation = id) Then
Assert.Fail("record is still there, transaction did not roll back")
End If
End Sub
ここで私が間違っていることを誰かが説明できますか? ロールバックどころか、トランザクションがないのはなぜですか?
更新 - コンテキスト クラス:
Public Class WebPersistentDataContext
Inherits DbContext
Public Property WebsiteErrors As DbSet(Of WebsiteError)
Public Property Websites As DbSet(Of Website)
Public Property WebSettings As DbSet(Of WebSetting)
Public Sub New()
End Sub
End Class