Ef 使用相似sqlserver 的nolock 查詢擴展類sql
/// <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-nolockasync