在过去的几个小时里,我一直在谷歌搜索答案,但还没有找到答案,所以我希望这里有人能指出我正确的方向。
我想知道如何在 TransactionScope 中使用 EF DbContext(代码优先)进行脏读。例如
DbContext context = new MyEntities();
using(TransactionScope scope = new TransactionScope())
{
context.SomeTable.Add(someObject);
context.SaveChanges();
var objects = context.SomeTable.Where(...).Select(...); //times out here on the read, because the write above locks the tables
//modify each object
context.SaveChanges();
scope.Complete(); //transaction is a success only if the entire batch succeeds
}
我尝试使用以下内容包装读取调用:
using(TransactionScope scope2 = new TransactionScope(TransactionScopeOption.RequiresNew, new TransactionOptions{IsolationLEvel = IsolationLevel.ReadUncommitted}))
{
var objects = context.SomeTable.Where(...).Select(...); //times out here on the
}
什么是正确的方法?