From 5e1dfb1d6bec4df29b55fe58872be3cde4100445 Mon Sep 17 00:00:00 2001 From: maliming Date: Thu, 29 Jun 2023 17:20:21 +0800 Subject: [PATCH] Log the entries info when `DbUpdateConcurrencyException` occurs. ```cs fail: Microsoft.EntityFrameworkCore.Update[10000] An exception occurred in the database while saving changes for context type 'Volo.Abp.TestApp.EntityFrameworkCore.TestAppDbContext'. Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException: The database operation was expected to affect 1 row(s), but actually affected 0 row(s); data may have been modified or deleted since entities were loaded. See http://go.microsoft.com/fwlink/?LinkId=527962 for information on understanding and handling optimistic concurrency exceptions. at Microsoft.EntityFrameworkCore.Update.AffectedCountModificationCommandBatch.ThrowAggregateUpdateConcurrencyExceptionAsync(RelationalDataReader reader, Int32 commandIndex, Int32 expectedRowsAffected, Int32 rowsAffected, CancellationToken cancellationToken) warn: Volo.Abp.EntityFrameworkCore.AbpDbContext[0] There is an entry which is not saved due to concurrency exception: City {Id: 27237527-605e-4652-a2a5-68e0e512da36} Deleted ``` --- .../Volo/Abp/EntityFrameworkCore/AbpDbContext.cs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs index 0a5eaa851e..1803e4acfe 100644 --- a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs +++ b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs @@ -6,6 +6,7 @@ using System.Globalization; using System.Linq; using System.Linq.Expressions; using System.Reflection; +using System.Text; using System.Threading; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; @@ -181,6 +182,20 @@ public abstract class AbpDbContext : DbContext, IAbpEfCoreDbContext, } catch (DbUpdateConcurrencyException ex) { + if (ex.Entries.Count > 0) + { + var sb = new StringBuilder(); + sb.AppendLine(ex.Entries.Count > 1 + ? "There are some entries which are not saved due to concurrency exception:" + : "There is an entry which is not saved due to concurrency exception:"); + foreach (var entry in ex.Entries) + { + sb.AppendLine(entry.ToString()); + } + + Logger.LogWarning(sb.ToString()); + } + throw new AbpDbConcurrencyException(ex.Message, ex); } finally