From cde7446d325def8730552d53133d90db64bca7c3 Mon Sep 17 00:00:00 2001 From: Huang Tao Date: Fri, 27 Oct 2023 16:02:10 +0800 Subject: [PATCH] performance: multiple attach on Update with EfCore --- .../Repositories/EntityFrameworkCore/EfCoreRepository.cs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Domain/Repositories/EntityFrameworkCore/EfCoreRepository.cs b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Domain/Repositories/EntityFrameworkCore/EfCoreRepository.cs index 3274360ba2..d73e18615d 100644 --- a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Domain/Repositories/EntityFrameworkCore/EfCoreRepository.cs +++ b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Domain/Repositories/EntityFrameworkCore/EfCoreRepository.cs @@ -161,16 +161,17 @@ public class EfCoreRepository : RepositoryBase, IE { var dbContext = await GetDbContextAsync(); - dbContext.Attach(entity); - - var updatedEntity = dbContext.Update(entity).Entity; + if (dbContext.Set().Local.All(e => e != entity)) + { + dbContext.Update(entity); + } if (autoSave) { await dbContext.SaveChangesAsync(GetCancellationToken(cancellationToken)); } - return updatedEntity; + return entity; } public async override Task UpdateManyAsync(IEnumerable entities, bool autoSave = false, CancellationToken cancellationToken = default)