带有NOLOCK的实体框架

如何NOLOCK在实体框架上使用该功能?XML是做到这一点的唯一方法吗?



慕尼黑的夜晚无繁华
浏览 303回答 3
3回答

青春有我

否,但是您可以启动事务并将隔离级别设置为uncommited。这本质上与NOLOCK相同,但是它不是针对每个表执行此操作,而是针对事务范围内的所有操作执行此操作。如果这听起来像您想要的,那么您可以按照以下方法进行操作...//declare the transaction optionsvar transactionOptions = new System.Transactions.TransactionOptions();//set it to read uncommitedtransactionOptions.IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted;//create the transaction scope, passing our options inusing (var transactionScope = new System.Transactions.TransactionScope(    System.Transactions.TransactionScopeOption.Required,     transactionOptions))//declare our contextusing (var context = new MyEntityConnection()){    //any reads we do here will also read uncomitted data    //...    //...    //don't forget to complete the transaction scope    transactionScope.Complete();}

一只名叫tom的猫

扩展方法可以使此操作更容易public static List<T> ToListReadUncommitted<T>(this IQueryable<T> query){&nbsp; &nbsp; using (var scope = new TransactionScope(&nbsp; &nbsp; &nbsp; &nbsp; TransactionScopeOption.Required,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; new TransactionOptions() {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted }))&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; List<T> toReturn = query.ToList();&nbsp; &nbsp; &nbsp; &nbsp; scope.Complete();&nbsp; &nbsp; &nbsp; &nbsp; return toReturn;&nbsp; &nbsp; }}public static int CountReadUncommitted<T>(this IQueryable<T> query){&nbsp; &nbsp; using (var scope = new TransactionScope(&nbsp; &nbsp; &nbsp; &nbsp; TransactionScopeOption.Required,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; new TransactionOptions() {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted }))&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; int toReturn = query.Count();&nbsp; &nbsp; &nbsp; &nbsp; scope.Complete();&nbsp; &nbsp; &nbsp; &nbsp; return toReturn;&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP