Skip to content

Commit 1794253

Browse files
authored
Merge pull request #1 from ozantopal/master
Fix for proper api usage
2 parents f599cba + f2598dd commit 1794253

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

EnLock/EnExtention.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using Microsoft.EntityFrameworkCore;
2+
using System;
23
using System.Collections.Generic;
34
using System.Linq;
5+
using System.Linq.Expressions;
46
using System.Threading;
57
using System.Threading.Tasks;
68
using System.Transactions;
@@ -69,6 +71,21 @@ public static async Task<T> ToFirstOrDefaultWithNoLockAsync<T>(this IQueryable<T
6971
}
7072
return result;
7173
}
74+
public static async Task<T> ToFirstOrDefaultWithNoLockAsync<T>(this IQueryable<T> query, Expression<Func<T, bool>> predicate, CancellationToken cancellationToken = default)
75+
{
76+
T result = default;
77+
using (var scope = new TransactionScope(TransactionScopeOption.Required,
78+
new TransactionOptions()
79+
{
80+
IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted
81+
},
82+
TransactionScopeAsyncFlowOption.Enabled))
83+
{
84+
result = await query.FirstOrDefaultAsync(predicate, cancellationToken);
85+
scope.Complete();
86+
}
87+
return result;
88+
}
7289
public static async Task<T> ToFirstWithNoLockAsync<T>(this IQueryable<T> query, CancellationToken cancellationToken = default)
7390
{
7491
T result = default;

0 commit comments

Comments
 (0)