为了账号安全,请及时绑定邮箱和手机立即绑定

带有NOLOCK的实体框架

带有NOLOCK的实体框架

C#
慕妹3146593 2019-12-10 09:08:50
如何NOLOCK在实体框架上使用该功能?XML是做到这一点的唯一方法吗?
查看完整描述

3 回答

?
子衿沉夜

TA贡献1828条经验 获得超3个赞

否,但是您可以启动事务并将隔离级别设置为uncommited。这本质上与NOLOCK相同,但是它不是针对每个表执行此操作,而是针对事务范围内的所有操作执行此操作。


如果这听起来像您想要的,那么您可以按照以下方法进行操作...


//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();

}



查看完整回答
反对 回复 2019-12-11
?
胡说叔叔

TA贡献1804条经验 获得超8个赞

扩展方法可以使此操作更容易


public static List<T> ToListReadUncommitted<T>(this IQueryable<T> query)

{

    using (var scope = new TransactionScope(

        TransactionScopeOption.Required, 

        new TransactionOptions() { 

            IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted }))

    {

        List<T> toReturn = query.ToList();

        scope.Complete();

        return toReturn;

    }

}


public static int CountReadUncommitted<T>(this IQueryable<T> query)

{

    using (var scope = new TransactionScope(

        TransactionScopeOption.Required, 

        new TransactionOptions() { 

            IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted }))

    {

        int toReturn = query.Count();

        scope.Complete();

        return toReturn;

    }

}



查看完整回答
反对 回复 2019-12-11
  • 3 回答
  • 0 关注
  • 308 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信