Browse Source

EF扩展WithNoLock系列函数增加同步调用版

懒得勤快 1 year ago
parent
commit
1965e45e52

+ 1 - 1
Masuit.Tools.AspNetCore/Masuit.Tools.AspNetCore.csproj

@@ -18,7 +18,7 @@
         <Product>Masuit.Tools.AspNetCore</Product>
         <PackageId>Masuit.Tools.AspNetCore</PackageId>
         <LangVersion>latest</LangVersion>
-        <Version>1.2.8.6</Version>
+        <Version>1.2.8.7</Version>
         <RepositoryType></RepositoryType>
         <GeneratePackageOnBuild>True</GeneratePackageOnBuild>
         <FileVersion>1.1.9</FileVersion>

+ 112 - 2
Masuit.Tools.Core/Database/DbContextExt.cs

@@ -221,6 +221,17 @@ public static class DbContextExt
         return result;
     }
 
+    public static List<T> ToListWithNoLock<T>(this IQueryable<T> query)
+    {
+        using var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions()
+        {
+            IsolationLevel = IsolationLevel.ReadUncommitted
+        }, TransactionScopeAsyncFlowOption.Enabled);
+        var result = query.ToList();
+        scope.Complete();
+        return result;
+    }
+
     public static async Task<int> CountWithNoLockAsync<T>(this IQueryable<T> query, CancellationToken cancellationToken = default)
     {
         using var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions()
@@ -232,6 +243,17 @@ public static class DbContextExt
         return result;
     }
 
+    public static int CountWithNoLock<T>(this IQueryable<T> query)
+    {
+        using var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions()
+        {
+            IsolationLevel = IsolationLevel.ReadUncommitted
+        }, TransactionScopeAsyncFlowOption.Enabled);
+        var result = query.Count();
+        scope.Complete();
+        return result;
+    }
+
     public static async Task<int> CountWithNoLockAsync<T>(this IQueryable<T> query, Expression<Func<T, bool>> where, CancellationToken cancellationToken = default)
     {
         using var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions()
@@ -243,6 +265,17 @@ public static class DbContextExt
         return result;
     }
 
+    public static int CountWithNoLock<T>(this IQueryable<T> query, Expression<Func<T, bool>> where)
+    {
+        using var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions()
+        {
+            IsolationLevel = IsolationLevel.ReadUncommitted
+        }, TransactionScopeAsyncFlowOption.Enabled);
+        var result = query.Count(where);
+        scope.Complete();
+        return result;
+    }
+
     public static async Task<bool> AnyWithNoLockAsync<T>(this IQueryable<T> query, CancellationToken cancellationToken = default)
     {
         using var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions()
@@ -254,6 +287,17 @@ public static class DbContextExt
         return result;
     }
 
+    public static bool AnyWithNoLock<T>(this IQueryable<T> query)
+    {
+        using var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions()
+        {
+            IsolationLevel = IsolationLevel.ReadUncommitted
+        }, TransactionScopeAsyncFlowOption.Enabled);
+        var result = query.Any();
+        scope.Complete();
+        return result;
+    }
+
     public static async Task<bool> AnyWithNoLockAsync<T>(this IQueryable<T> query, Expression<Func<T, bool>> where, CancellationToken cancellationToken = default)
     {
         using var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions()
@@ -265,6 +309,17 @@ public static class DbContextExt
         return result;
     }
 
+    public static bool AnyWithNoLock<T>(this IQueryable<T> query, Expression<Func<T, bool>> where)
+    {
+        using var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions()
+        {
+            IsolationLevel = IsolationLevel.ReadUncommitted
+        }, TransactionScopeAsyncFlowOption.Enabled);
+        var result = query.Any(where);
+        scope.Complete();
+        return result;
+    }
+
     public static async Task<T> FirstOrDefaultWithNoLockAsync<T>(this IQueryable<T> query, Expression<Func<T, bool>> where, CancellationToken cancellationToken = default)
     {
         using var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions()
@@ -276,20 +331,42 @@ public static class DbContextExt
         return result;
     }
 
-    public static async Task<T> FirstOrDefaultWithNoLockAsync<T>(this IQueryable<T> query, CancellationToken cancellationToken = default)
+    public static T FirstOrDefaultWithNoLock<T>(this IQueryable<T> query, Expression<Func<T, bool>> where)
     {
         using var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions()
         {
             IsolationLevel = IsolationLevel.ReadUncommitted
         }, TransactionScopeAsyncFlowOption.Enabled);
+        var result = query.FirstOrDefault(where);
+        scope.Complete();
+        return result;
+    }
+
+    public static async Task<T> FirstOrDefaultWithNoLockAsync<T>(this IQueryable<T> query, CancellationToken cancellationToken = default)
+    {
+        using var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions
+        {
+            IsolationLevel = IsolationLevel.ReadUncommitted
+        }, TransactionScopeAsyncFlowOption.Enabled);
         var result = await query.FirstOrDefaultAsync(cancellationToken);
         scope.Complete();
         return result;
     }
 
+    public static T FirstOrDefaultWithNoLock<T>(this IQueryable<T> query)
+    {
+        using var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions
+        {
+            IsolationLevel = IsolationLevel.ReadUncommitted
+        }, TransactionScopeAsyncFlowOption.Enabled);
+        var result = query.FirstOrDefault();
+        scope.Complete();
+        return result;
+    }
+
     public static async Task<T> SingleOrDefaultWithNoLockAsync<T>(this IQueryable<T> query, Expression<Func<T, bool>> where, CancellationToken cancellationToken = default)
     {
-        using var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions()
+        using var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions
         {
             IsolationLevel = IsolationLevel.ReadUncommitted
         }, TransactionScopeAsyncFlowOption.Enabled);
@@ -298,6 +375,17 @@ public static class DbContextExt
         return result;
     }
 
+    public static T SingleOrDefaultWithNoLock<T>(this IQueryable<T> query, Expression<Func<T, bool>> where)
+    {
+        using var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions()
+        {
+            IsolationLevel = IsolationLevel.ReadUncommitted
+        }, TransactionScopeAsyncFlowOption.Enabled);
+        var result = query.SingleOrDefault(where);
+        scope.Complete();
+        return result;
+    }
+
     public static async Task<T> SingleOrDefaultWithNoLockAsync<T>(this IQueryable<T> query, CancellationToken cancellationToken = default)
     {
         using var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions()
@@ -309,6 +397,17 @@ public static class DbContextExt
         return result;
     }
 
+    public static T SingleOrDefaultWithNoLock<T>(this IQueryable<T> query)
+    {
+        using var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions()
+        {
+            IsolationLevel = IsolationLevel.ReadUncommitted
+        }, TransactionScopeAsyncFlowOption.Enabled);
+        var result = query.SingleOrDefault();
+        scope.Complete();
+        return result;
+    }
+
     public static async Task<bool> AllWithNoLockAsync<T>(this IQueryable<T> query, Expression<Func<T, bool>> where, CancellationToken cancellationToken = default)
     {
         using var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions()
@@ -320,6 +419,17 @@ public static class DbContextExt
         return result;
     }
 
+    public static bool AllWithNoLock<T>(this IQueryable<T> query, Expression<Func<T, bool>> where)
+    {
+        using var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions()
+        {
+            IsolationLevel = IsolationLevel.ReadUncommitted
+        }, TransactionScopeAsyncFlowOption.Enabled);
+        var result = query.All(where);
+        scope.Complete();
+        return result;
+    }
+
     public static T NoLock<T, TDbContext>(this TDbContext dbContext, Func<TDbContext, T> func) where TDbContext : DbContext
     {
         var strategy = dbContext.Database.CreateExecutionStrategy();

+ 1 - 1
Masuit.Tools.Core/Masuit.Tools.Core.csproj

@@ -6,7 +6,7 @@
 官网教程:https://tools.masuit.org
 github:https://github.com/ldqk/Masuit.Tools
         </Description>
-        <Version>2.6.8.6</Version>
+        <Version>2.6.8.7</Version>
         <Copyright>Copyright © 懒得勤快</Copyright>
         <PackageProjectUrl>https://github.com/ldqk/Masuit.Tools</PackageProjectUrl>
         <PackageTags>Masuit.Tools,工具库,Utility,Crypt,Extensions</PackageTags>

+ 2 - 2
Test/Masuit.Tools.Abstractions.Test/Masuit.Tools.Abstractions.Test.csproj

@@ -14,8 +14,8 @@
 
   <ItemGroup>
     <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
-    <PackageReference Include="xunit" Version="2.6.3" />
-    <PackageReference Include="xunit.runner.visualstudio" Version="2.5.5">
+    <PackageReference Include="xunit" Version="2.6.4" />
+    <PackageReference Include="xunit.runner.visualstudio" Version="2.5.6">
       <PrivateAssets>all</PrivateAssets>
       <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
     </PackageReference>

+ 2 - 2
Test/Masuit.Tools.Core.Test/Masuit.Tools.Core.Test.csproj

@@ -12,8 +12,8 @@
     <PackageReference Include="Microsoft.AspNetCore.TestHost" Version="8.0.0" />
     <PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
     <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
-    <PackageReference Include="xunit" Version="2.6.3" />
-    <PackageReference Include="xunit.runner.visualstudio" Version="2.5.5">
+    <PackageReference Include="xunit" Version="2.6.4" />
+    <PackageReference Include="xunit.runner.visualstudio" Version="2.5.6">
       <PrivateAssets>all</PrivateAssets>
       <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
     </PackageReference>

+ 6 - 6
Test/Masuit.Tools.Test/Masuit.Tools.Test.csproj

@@ -116,25 +116,25 @@
       <Version>4.5.0</Version>
     </PackageReference>
     <PackageReference Include="xunit">
-      <Version>2.6.3</Version>
+      <Version>2.6.4</Version>
     </PackageReference>
     <PackageReference Include="xunit.abstractions">
       <Version>2.0.3</Version>
     </PackageReference>
     <PackageReference Include="xunit.analyzers">
-      <Version>1.7.0</Version>
+      <Version>1.8.0</Version>
     </PackageReference>
     <PackageReference Include="xunit.assert">
-      <Version>2.6.3</Version>
+      <Version>2.6.4</Version>
     </PackageReference>
     <PackageReference Include="xunit.core">
-      <Version>2.6.3</Version>
+      <Version>2.6.4</Version>
     </PackageReference>
     <PackageReference Include="xunit.extensibility.core">
-      <Version>2.6.3</Version>
+      <Version>2.6.4</Version>
     </PackageReference>
     <PackageReference Include="xunit.extensibility.execution">
-      <Version>2.6.3</Version>
+      <Version>2.6.4</Version>
     </PackageReference>
   </ItemGroup>
   <Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" />