Skip to content

Commit

Permalink
General nolock function added
Browse files Browse the repository at this point in the history
  • Loading branch information
enisgurkann committed Jan 18, 2022
1 parent 0d9b962 commit 959da7c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
15 changes: 15 additions & 0 deletions EnLock.Test/UnitTest1.cs → EnLock.Test/ExtentionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,21 @@ public async Task List_Test()
}
}

[Fact]
public async Task NoLock_Test()
{
using (var context = new TestContext(CreateNewContextOptions()))
{
await _testContext.TestDbSet.AddRangeAsync(GetDbSet());
await _testContext.SaveChangesAsync();

var list = await _testContext
.NoLock(s => s.TestDbSet.ToListAsync());

Assert.Equal(list.Count(), 3);
}
}

[Fact]
public async Task Any_Test()
{
Expand Down
26 changes: 26 additions & 0 deletions EnLock/EnExtention.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,5 +162,31 @@ public static async Task<T> ToSingleWithNoLockAsync<T>(this IQueryable<T> query,
}
return result;
}


#region NoLockFunc
/// <summary>
/// No Lock tag added
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="TDbContext"></typeparam>
/// <param name="dbContext"></param>
/// <param name="func"></param>
/// <returns></returns>
public static T NoLock<T, TDbContext>(this TDbContext dbContext, Func<TDbContext, T> func) where TDbContext : DbContext
{
T result = default;
var transactionOptions = new TransactionOptions
{
IsolationLevel = IsolationLevel.ReadUncommitted
};
using (var scope = new TransactionScope(TransactionScopeOption.Required, transactionOptions))
{
result = func(dbContext);
scope.Complete();
}
return result;
}
#endregion
}
}

0 comments on commit 959da7c

Please sign in to comment.