From 66a620b7d97550c52cb3538cae9f9af88bdbba09 Mon Sep 17 00:00:00 2001 From: Sipke Schoorstra Date: Wed, 28 Feb 2024 09:12:00 +0100 Subject: [PATCH] Disable Dapper and integrate EFCore.BulkExtensions.MIT package (#5005) The changes involve turning off the use of Dapper in Elsa.Server.Web project for database operations. In addition, the `EFCore.BulkExtensions.MIT` has been added in the Elsa.EntityFrameworkCore.Common project as a package reference. The BulkUpsertAsync function has been refactored to utilize the BulkInsertOrUpdateAsync function provided by the BulkExtensions package, streamlining the operation. The related package versions have also been introduced in the Directory.Packages.props file. --- Directory.Packages.props | 16 +++++++------ src/bundles/Elsa.Server.Web/Program.cs | 2 +- .../Elsa.EntityFrameworkCore.Common.csproj | 1 + .../Extensions/QueryableExtensions.cs | 23 ++++--------------- 4 files changed, 16 insertions(+), 26 deletions(-) diff --git a/Directory.Packages.props b/Directory.Packages.props index 302aca426..29df7d6dd 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -95,14 +95,15 @@ - - - - - + + + + + + @@ -134,6 +135,7 @@ + @@ -160,7 +162,7 @@ - + - + \ No newline at end of file diff --git a/src/bundles/Elsa.Server.Web/Program.cs b/src/bundles/Elsa.Server.Web/Program.cs index cf1b19952..b0d4b8c7f 100644 --- a/src/bundles/Elsa.Server.Web/Program.cs +++ b/src/bundles/Elsa.Server.Web/Program.cs @@ -25,7 +25,7 @@ using Proto.Persistence.SqlServer; const bool useMongoDb = false; const bool useSqlServer = false; -const bool useDapper = true; +const bool useDapper = false; const bool useProtoActor = false; const bool useHangfire = false; const bool useQuartz = true; diff --git a/src/modules/Elsa.EntityFrameworkCore.Common/Elsa.EntityFrameworkCore.Common.csproj b/src/modules/Elsa.EntityFrameworkCore.Common/Elsa.EntityFrameworkCore.Common.csproj index 6c01211d5..1cd34ddc1 100644 --- a/src/modules/Elsa.EntityFrameworkCore.Common/Elsa.EntityFrameworkCore.Common.csproj +++ b/src/modules/Elsa.EntityFrameworkCore.Common/Elsa.EntityFrameworkCore.Common.csproj @@ -8,6 +8,7 @@ + diff --git a/src/modules/Elsa.EntityFrameworkCore.Common/Extensions/QueryableExtensions.cs b/src/modules/Elsa.EntityFrameworkCore.Common/Extensions/QueryableExtensions.cs index 28be0c676..c0b815617 100644 --- a/src/modules/Elsa.EntityFrameworkCore.Common/Extensions/QueryableExtensions.cs +++ b/src/modules/Elsa.EntityFrameworkCore.Common/Extensions/QueryableExtensions.cs @@ -1,4 +1,5 @@ using System.Linq.Expressions; +using EFCore.BulkExtensions; using Elsa.Common.Models; using JetBrains.Annotations; using Microsoft.EntityFrameworkCore; @@ -17,24 +18,10 @@ public static class QueryableExtensions /// public static async Task BulkUpsertAsync(this TDbContext dbContext, IList entities, Expression> keySelector, CancellationToken cancellationToken = default) where TDbContext : DbContext where TEntity : class { - var set = dbContext.Set(); - var compiledKeySelector = keySelector.Compile(); - var containsLambda = entities.Any() ? keySelector.BuildContainsExpression(entities) : default; - var existingEntitiesQuery = set.AsNoTracking(); - - if (containsLambda != null) - existingEntitiesQuery = existingEntitiesQuery.Where(containsLambda); - - var existingEntities = await existingEntitiesQuery.ToListAsync(cancellationToken); - var entitiesToUpdate = entities.IntersectBy(existingEntities.Select(compiledKeySelector), compiledKeySelector).ToList(); - var entitiesToInsert = entities.Except(entitiesToUpdate).ToList(); - - if (entitiesToUpdate.Any()) - set.UpdateRange(entitiesToUpdate); - if (entitiesToInsert.Any()) - await set.AddRangeAsync(entitiesToInsert, cancellationToken); - - await dbContext.SaveChangesAsync(cancellationToken); + await dbContext.BulkInsertOrUpdateAsync(entities, new BulkConfig + { + EnableShadowProperties = true + }, cancellationToken: cancellationToken); } ///