Browse Source

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

```
pull/16995/head
maliming 3 years ago
parent
commit
5e1dfb1d6b
No known key found for this signature in database GPG Key ID: A646B9CB645ECEA4
  1. 15
      framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs

15
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<TDbContext> : 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

Loading…
Cancel
Save