亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

帶有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 關注
  • 367 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號