|
| 1 | +using Microsoft.EntityFrameworkCore; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Threading; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using System.Transactions; |
| 7 | + |
| 8 | +namespace EnLock |
| 9 | +{ |
| 10 | + public static class EnExtention |
| 11 | + { |
| 12 | + public static async Task<bool> ToAnyWithNoLockAsync<T>(this IQueryable<T> query, CancellationToken cancellationToken = default) |
| 13 | + { |
| 14 | + bool result = default; |
| 15 | + using (var scope = new TransactionScope(TransactionScopeOption.Required, |
| 16 | + new TransactionOptions() |
| 17 | + { |
| 18 | + IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted |
| 19 | + }, |
| 20 | + TransactionScopeAsyncFlowOption.Enabled)) |
| 21 | + { |
| 22 | + result = await query.AnyAsync(cancellationToken); |
| 23 | + scope.Complete(); |
| 24 | + } |
| 25 | + return result; |
| 26 | + } |
| 27 | + public static async Task<T[]> ToArrayWithNoLockAsync<T>(this IQueryable<T> query, CancellationToken cancellationToken = default) |
| 28 | + { |
| 29 | + T[] result = default; |
| 30 | + using (var scope = new TransactionScope(TransactionScopeOption.Required, |
| 31 | + new TransactionOptions() |
| 32 | + { |
| 33 | + IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted |
| 34 | + }, |
| 35 | + TransactionScopeAsyncFlowOption.Enabled)) |
| 36 | + { |
| 37 | + result = await query.ToArrayAsync(cancellationToken); |
| 38 | + scope.Complete(); |
| 39 | + } |
| 40 | + return result; |
| 41 | + } |
| 42 | + public static async Task<List<T>> ToListWithNoLockAsync<T>(this IQueryable<T> query, CancellationToken cancellationToken = default) |
| 43 | + { |
| 44 | + List<T> result = default; |
| 45 | + using (var scope = new TransactionScope(TransactionScopeOption.Required, |
| 46 | + new TransactionOptions() |
| 47 | + { |
| 48 | + IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted |
| 49 | + }, |
| 50 | + TransactionScopeAsyncFlowOption.Enabled)) |
| 51 | + { |
| 52 | + result = await query.ToListAsync(cancellationToken); |
| 53 | + scope.Complete(); |
| 54 | + } |
| 55 | + return result; |
| 56 | + } |
| 57 | + public static async Task<T> ToFirstOrDefaultWithNoLockAsync<T>(this IQueryable<T> query, CancellationToken cancellationToken = default) |
| 58 | + { |
| 59 | + T result = default; |
| 60 | + using (var scope = new TransactionScope(TransactionScopeOption.Required, |
| 61 | + new TransactionOptions() |
| 62 | + { |
| 63 | + IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted |
| 64 | + }, |
| 65 | + TransactionScopeAsyncFlowOption.Enabled)) |
| 66 | + { |
| 67 | + result = await query.FirstOrDefaultAsync(cancellationToken); |
| 68 | + scope.Complete(); |
| 69 | + } |
| 70 | + return result; |
| 71 | + } |
| 72 | + public static async Task<T> ToFirstWithNoLockAsync<T>(this IQueryable<T> query, CancellationToken cancellationToken = default) |
| 73 | + { |
| 74 | + T result = default; |
| 75 | + using (var scope = new TransactionScope(TransactionScopeOption.Required, |
| 76 | + new TransactionOptions() |
| 77 | + { |
| 78 | + IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted |
| 79 | + }, |
| 80 | + TransactionScopeAsyncFlowOption.Enabled)) |
| 81 | + { |
| 82 | + result = await query.FirstOrDefaultAsync(cancellationToken); |
| 83 | + scope.Complete(); |
| 84 | + } |
| 85 | + return result; |
| 86 | + } |
| 87 | + public static async Task<T> ToSingleWithNoLockAsync<T>(this IQueryable<T> query, CancellationToken cancellationToken = default) |
| 88 | + { |
| 89 | + T result = default; |
| 90 | + using (var scope = new TransactionScope(TransactionScopeOption.Required, |
| 91 | + new TransactionOptions() |
| 92 | + { |
| 93 | + IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted |
| 94 | + }, |
| 95 | + TransactionScopeAsyncFlowOption.Enabled)) |
| 96 | + { |
| 97 | + result = await query.SingleAsync(cancellationToken); |
| 98 | + scope.Complete(); |
| 99 | + } |
| 100 | + return result; |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments