喬恩,不,你不需要使用TransactionScope。樂觀併發由Linq自動處理。您提供的鏈接中的代碼示例很好地說明了您不必自行回滾事務。我會使用與樣本中相同的代碼:
try
{
// Try to save changes, which may cause a conflict.
int num = context.SaveChanges();
Console.WriteLine("No conflicts. " +
num.ToString() + " updates saved.");
}
catch (OptimisticConcurrencyException)
{
// Resolve the concurrency conflict by refreshing the
// object context before re-saving changes.
context.Refresh(RefreshMode.ClientWins, orders);
// Save changes.
context.SaveChanges();
Console.WriteLine("OptimisticConcurrencyException "
+ "handled and changes saved");
}
請注意刷新,重新保存,它處理您的問題。你可以通過在try塊內拋出一個異常來測試。
問候