mirror of
https://github.com/meysamhadeli/booking-microservices.git
synced 2026-04-27 08:00:52 +08:00
fix: try for fix DbUpdateConcurrencyException
This commit is contained in:
parent
c09f854b28
commit
6dffdfb779
@ -13,6 +13,7 @@ using global::Polly;
|
|||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Exception = System.Exception;
|
||||||
|
|
||||||
public abstract class AppDbContextBase : DbContext, IDbContext
|
public abstract class AppDbContextBase : DbContext, IDbContext
|
||||||
{
|
{
|
||||||
@ -82,6 +83,7 @@ public abstract class AppDbContextBase : DbContext, IDbContext
|
|||||||
//ref: https://learn.microsoft.com/en-us/ef/core/saving/concurrency?tabs=fluent-api#resolving-concurrency-conflicts
|
//ref: https://learn.microsoft.com/en-us/ef/core/saving/concurrency?tabs=fluent-api#resolving-concurrency-conflicts
|
||||||
catch (DbUpdateConcurrencyException ex)
|
catch (DbUpdateConcurrencyException ex)
|
||||||
{
|
{
|
||||||
|
throw new DbUpdateConcurrencyException("try for get unhandled exception with DbUpdateConcurrencyException", ex);
|
||||||
var logger = _httpContextAccessor?.HttpContext?.RequestServices
|
var logger = _httpContextAccessor?.HttpContext?.RequestServices
|
||||||
.GetRequiredService<ILogger<AppDbContextBase>>();
|
.GetRequiredService<ILogger<AppDbContextBase>>();
|
||||||
|
|
||||||
@ -116,6 +118,10 @@ public abstract class AppDbContextBase : DbContext, IDbContext
|
|||||||
|
|
||||||
return await policy.ExecuteAsync(async () => await base.SaveChangesAsync(cancellationToken));
|
return await policy.ExecuteAsync(async () => await base.SaveChangesAsync(cancellationToken));
|
||||||
}
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
throw new Exception("try for get unhandled exception bt default", ex);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public IReadOnlyList<IDomainEvent> GetDomainEvents()
|
public IReadOnlyList<IDomainEvent> GetDomainEvents()
|
||||||
@ -139,36 +145,43 @@ public abstract class AppDbContextBase : DbContext, IDbContext
|
|||||||
// ref: https://www.meziantou.net/entity-framework-core-soft-delete-using-query-filters.htm
|
// ref: https://www.meziantou.net/entity-framework-core-soft-delete-using-query-filters.htm
|
||||||
private void OnBeforeSaving()
|
private void OnBeforeSaving()
|
||||||
{
|
{
|
||||||
foreach (var entry in ChangeTracker.Entries<IAggregate>())
|
try
|
||||||
{
|
{
|
||||||
var isAuditable = entry.Entity.GetType().IsAssignableTo(typeof(IAggregate));
|
foreach (var entry in ChangeTracker.Entries<IAggregate>())
|
||||||
var userId = GetCurrentUserId();
|
|
||||||
|
|
||||||
if (isAuditable)
|
|
||||||
{
|
{
|
||||||
switch (entry.State)
|
var isAuditable = entry.Entity.GetType().IsAssignableTo(typeof(IAggregate));
|
||||||
|
var userId = GetCurrentUserId();
|
||||||
|
|
||||||
|
if (isAuditable)
|
||||||
{
|
{
|
||||||
case EntityState.Added:
|
switch (entry.State)
|
||||||
entry.Entity.CreatedBy = userId;
|
{
|
||||||
entry.Entity.CreatedAt = DateTime.Now;
|
case EntityState.Added:
|
||||||
break;
|
entry.Entity.CreatedBy = userId;
|
||||||
|
entry.Entity.CreatedAt = DateTime.Now;
|
||||||
|
break;
|
||||||
|
|
||||||
case EntityState.Modified:
|
case EntityState.Modified:
|
||||||
entry.Entity.LastModifiedBy = userId;
|
entry.Entity.LastModifiedBy = userId;
|
||||||
entry.Entity.LastModified = DateTime.Now;
|
entry.Entity.LastModified = DateTime.Now;
|
||||||
entry.Entity.Version++;
|
entry.Entity.Version++;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case EntityState.Deleted:
|
case EntityState.Deleted:
|
||||||
entry.State = EntityState.Modified;
|
entry.State = EntityState.Modified;
|
||||||
entry.Entity.LastModifiedBy = userId;
|
entry.Entity.LastModifiedBy = userId;
|
||||||
entry.Entity.LastModified = DateTime.Now;
|
entry.Entity.LastModified = DateTime.Now;
|
||||||
entry.Entity.IsDeleted = true;
|
entry.Entity.IsDeleted = true;
|
||||||
entry.Entity.Version++;
|
entry.Entity.Version++;
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
throw new Exception("try for find IAggregate", ex);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private long? GetCurrentUserId()
|
private long? GetCurrentUserId()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user