• Entity Framework with NOLOCK


    Ef 使用类似sqlserver 的nolock 查询扩展类

        /// <summary>
        /// 类似SqlServer nolock 查询扩展
        /// Like SqlServer Nolock
        /// </summary>
        public static class NoLockQuery
        {
            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;
                }
            }
    
            public static async Task<List<T>> ToListReadUncommittedAsync<T>(this IQueryable<T> query)
            {
                using (var scope = new TransactionScope(
                    TransactionScopeOption.Required,
                    new TransactionOptions()
                    {
                        IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted
                    },
                      TransactionScopeAsyncFlowOption.Enabled
                    ))
                {
                    List<T> toReturn = await query.ToListAsync();
                    scope.Complete();
                    return toReturn;
                }
            }
    
            public static async Task<int> CountReadUncommittedAsync<T>(this IQueryable<T> query)
            {
                using (var scope = new TransactionScope(
                    TransactionScopeOption.Required,
                    new TransactionOptions()
                    {
                        IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted
                    },
                     TransactionScopeAsyncFlowOption.Enabled
                    ))
                {
                    int toReturn = await query.CountAsync();
                    scope.Complete();
                    return toReturn;
                }
            }
        }

    参考:http://stackoverflow.com/questions/926656/entity-framework-with-nolock

  • 相关阅读:
    HDU 4221 Greedy?(贪心)
    HDU 3400 Line belt (三分法)
    POJ 1026 Cipher(置换)
    POJ 1166 The Clocks(暴搜)
    HDU 4122 Alice's mooncake shop(RMQ,或者单调队列)
    POJ 1721 CARDS(置换)
    POJ 3270 Cow Sorting(置换)
    高斯消元法(模板)
    http://blog.seirsoft.com
    转载 STL容器的erase用法
  • 原文地址:https://www.cnblogs.com/miralce/p/6598067.html
Copyright © 2020-2023  润新知