实体框架与NOLOCK [英] Entity Framework with NOLOCK

查看:121
本文介绍了实体框架与NOLOCK的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何可以使用实体框架的NOLOCK功能? XML是做到这一点的唯一途径?

How can I use the NOLOCK function on Entity Framework? Is XML the only way to do this?

推荐答案

没有,但你可以开始一个事务,设置的隔离级别读取未提交。这实质上是不一样的NOLOCK,但不是对每个表的基础上做的,它会做的交易范围之内的一切。

No, but you can start a transaction and set the isolation level to read uncommited. This essentially does the same as NOLOCK, but instead of doing it on a per table basis, it will do it for everything within the scope of the transaction.

如果这听起来像是你想要的东西,这里是你如何能去这样做......

If that sounds like what you want, here's how you could go about doing it...

//declare the transaction options
var transactionOptions = new System.Transactions.TransactionOptions();
//set it to read uncommited
transactionOptions.IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted;
//create the transaction scope, passing our options in
using (var transactionScope = new System.Transactions.TransactionScope(
    System.Transactions.TransactionScopeOption.Required, 
    transactionOptions)
)

//declare our context
using (var context = new MyEntityConnection())
{
    //any reads we do here will also read uncomitted data
    //...
    //...
    //don't forget to complete the transaction scope
    transactionScope.Complete();
}

这篇关于实体框架与NOLOCK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆