using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Transactions; namespace EasyDevCore.Database.EntityFrameworkCore { /// /// /// public static class QueryUtilsExtensions { /// /// Executes the with nolock. /// /// /// /// The query. /// The command. /// public static R ExecWithNolock(this IQueryable query, Func, R> command) { using(var tran = CreateNolockScope(null)) { var result = command(query); tran.Complete(); return result; } } /// /// Executes the with nolock asynchronous. /// /// /// /// The query. /// The command. /// public static async Task ExecWithNolockAsync(this IQueryable query, Func, Task> command) { using (var tran = CreateNolockScopeAsync(null)) { var result = await command(query); tran.Complete(); return result; } } /// /// Creates the nolock scope. /// /// The context. /// public static TransactionScope CreateNolockScope(this EasyDbContext context) { return new TransactionScope(TransactionScopeOption.Required, new TransactionOptions() { IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted }); } /// /// Creates the nolock scope asynchronous. /// /// The context. /// public static TransactionScope CreateNolockScopeAsync(this EasyDbContext context) { return new TransactionScope(TransactionScopeOption.Required, new TransactionOptions() { IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted }, TransactionScopeAsyncFlowOption.Enabled); } /// /// Firsts the or default with nolock. /// /// /// The query. /// public static T FirstOrDefaultWithNolock(this IQueryable query) { return query.ExecWithNolock(q => q.FirstOrDefault()); } /// /// Firsts the or default with nolock asynchronous. /// /// /// The query. /// public static async Task FirstOrDefaultWithNolockAsync(this IQueryable query) { return await query.ExecWithNolockAsync(q => q.FirstOrDefaultAsync()); } /// /// Firsts the with nolock. /// /// /// The query. /// public static T FirstWithNolock(this IQueryable query) { return query.ExecWithNolock(q => q.First()); } /// /// Firsts the with nolock asynchronous. /// /// /// The query. /// public static async Task FirstWithNolockAsync(this IQueryable query) { return await query.ExecWithNolockAsync(q => q.FirstOrDefaultAsync()); } /// /// Converts to listwithnolock. /// /// /// The query. /// public static List ToListWithNolock(this IQueryable query) { return query.ExecWithNolock(q => q.ToList()); } /// /// Converts to listwithnolockasync. /// /// /// The query. /// public static async Task> ToListWithNolockAsync(this IQueryable query) { return await query.ExecWithNolockAsync(q => q.ToListAsync()); } /// /// Converts to arraywithnolock. /// /// /// The query. /// public static T[] ToArrayWithNolock(this IQueryable query) { return query.ExecWithNolock(q => q.ToArray()); } /// /// Converts to arraywithnolockasync. /// /// /// The query. /// public static async Task ToArrayWithNolockAsync(this IQueryable query) { return await query.ExecWithNolockAsync(q => q.ToArrayAsync()); } /// /// Counts the with nolock. /// /// /// The query. /// public static int CountWithNolock(this IQueryable query) { return query.ExecWithNolock(q => q.Count()); } /// /// Counts the with nolock asynchronous. /// /// /// The query. /// public static async Task CountWithNolockAsync(this IQueryable query) { return await query.ExecWithNolockAsync(q => q.CountAsync()); } /// /// Longs the count with nolock. /// /// /// The query. /// public static long LongCountWithNolock(this IQueryable query) { return query.ExecWithNolock(q => q.LongCount()); } /// /// Longs the count with nolock asynchronous. /// /// /// The query. /// public static async Task LongCountWithNolockAsync(this IQueryable query) { return await query.ExecWithNolockAsync(q => q.LongCountAsync()); } } }