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.
This commit is contained in:
Sipke Schoorstra 2024-02-28 09:12:00 +01:00 committed by GitHub
parent d0736e5e88
commit 66a620b7d9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 16 additions and 26 deletions

View file

@ -95,14 +95,15 @@
<PackageVersion Include="xunit.abstractions" Version="2.0.3" />
<PackageVersion Include="xunit.extensibility.core" Version="2.6.2" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.5.4" />
<PackageVersion Include="AppAny.Quartz.EntityFrameworkCore.Migrations" Version="0.5.0" />
<PackageVersion Include="AppAny.Quartz.EntityFrameworkCore.Migrations.MySql" Version="0.5.0" />
<PackageVersion Include="AppAny.Quartz.EntityFrameworkCore.Migrations.PostgreSQL" Version="0.5.0" />
<PackageVersion Include="AppAny.Quartz.EntityFrameworkCore.Migrations.SQLite" Version="0.5.0" />
<PackageVersion Include="AppAny.Quartz.EntityFrameworkCore.Migrations.SqlServer" Version="0.5.0" />
<PackageVersion Include="AppAny.Quartz.EntityFrameworkCore.Migrations" Version="0.5.0" />
<PackageVersion Include="AppAny.Quartz.EntityFrameworkCore.Migrations.MySql" Version="0.5.0" />
<PackageVersion Include="AppAny.Quartz.EntityFrameworkCore.Migrations.PostgreSQL" Version="0.5.0" />
<PackageVersion Include="AppAny.Quartz.EntityFrameworkCore.Migrations.SQLite" Version="0.5.0" />
<PackageVersion Include="AppAny.Quartz.EntityFrameworkCore.Migrations.SqlServer" Version="0.5.0" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net6.0' or '$(TargetFramework)' == 'net7.0'">
<PackageVersion Include="AspNetCore.Authentication.ApiKey" Version="7.0.0" />
<PackageVersion Include="EFCore.BulkExtensions.MIT" Version="7.16.0" />
<PackageVersion Include="Microsoft.AspNetCore.Authorization" Version="7.0.14" />
<PackageVersion Include="Microsoft.AspNetCore.Components.WebAssembly" Version="7.0.14" />
<PackageVersion Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="7.0.14" />
@ -134,6 +135,7 @@
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
<PackageVersion Include="AspNetCore.Authentication.ApiKey" Version="8.0.0" />
<PackageVersion Include="EFCore.BulkExtensions.MIT" Version="8.16.0" />
<PackageVersion Include="Microsoft.AspNetCore.Authorization" Version="8.0.0" />
<PackageVersion Include="Microsoft.AspNetCore.Components.WebAssembly" Version="8.0.0" />
<PackageVersion Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="8.0.0" />
@ -160,7 +162,7 @@
<PackageVersion Include="Npgsql" Version="8.0.0" />
<PackageVersion Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.0" />
<PackageVersion Include="Polly" Version="8.2.0" />
<PackageVersion Include="Pomelo.EntityFrameworkCore.MySql" Version="8.0.0-beta.2" />
<PackageVersion Include="Pomelo.EntityFrameworkCore.MySql" Version="8.0.0" />
<PackageVersion Include="System.Text.Json" Version="8.0.0" />
</ItemGroup>
</Project>
</Project>

View file

@ -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;

View file

@ -8,6 +8,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="EFCore.BulkExtensions.MIT" />
<PackageReference Include="Microsoft.EntityFrameworkCore" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" PrivateAssets="all" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" />

View file

@ -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
/// </summary>
public static async Task BulkUpsertAsync<TDbContext, TEntity>(this TDbContext dbContext, IList<TEntity> entities, Expression<Func<TEntity, string>> keySelector, CancellationToken cancellationToken = default) where TDbContext : DbContext where TEntity : class
{
var set = dbContext.Set<TEntity>();
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);
}
/// <summary>