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 9b488924e8..8c18b565d7 100644 --- a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs +++ b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs @@ -149,6 +149,8 @@ namespace Volo.Abp.EntityFrameworkCore { try { + var changeReport = ApplyAbpConcepts(); + var auditLog = AuditingManager?.Current?.Log; List entityChangeList = null; @@ -157,8 +159,6 @@ namespace Volo.Abp.EntityFrameworkCore entityChangeList = EntityHistoryHelper.CreateChangeList(ChangeTracker.Entries().ToList()); } - var changeReport = ApplyAbpConcepts(); - var result = await base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken); await EntityChangeEventHelper.TriggerEventsAsync(changeReport); diff --git a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/EntityHistory/EntityHistoryHelper.cs b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/EntityHistory/EntityHistoryHelper.cs index 4ef6057943..d7dca36439 100644 --- a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/EntityHistory/EntityHistoryHelper.cs +++ b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/EntityHistory/EntityHistoryHelper.cs @@ -166,7 +166,7 @@ namespace Volo.Abp.EntityFrameworkCore.EntityHistory foreach (var property in properties) { var propertyEntry = entityEntry.Property(property.Name); - if (ShouldSavePropertyHistory(propertyEntry, isCreated || isDeleted)) + if (ShouldSavePropertyHistory(propertyEntry, isCreated || isDeleted) && !IsSoftDeleted(entityEntry)) { propertyChanges.Add(new EntityPropertyChangeInfo { @@ -188,11 +188,11 @@ namespace Volo.Abp.EntityFrameworkCore.EntityHistory protected virtual bool IsDeleted(EntityEntry entityEntry) { - if (entityEntry.State == EntityState.Deleted) - { - return true; - } + return entityEntry.State == EntityState.Deleted || IsSoftDeleted(entityEntry); + } + protected virtual bool IsSoftDeleted(EntityEntry entityEntry) + { var entity = entityEntry.Entity; return entity is ISoftDelete && entity.As().IsDeleted; } diff --git a/framework/test/Volo.Abp.Auditing.Tests/Volo/Abp/Auditing/AbpAuditingTestModule.cs b/framework/test/Volo.Abp.Auditing.Tests/Volo/Abp/Auditing/AbpAuditingTestModule.cs index ad7b62902d..41d35ebadc 100644 --- a/framework/test/Volo.Abp.Auditing.Tests/Volo/Abp/Auditing/AbpAuditingTestModule.cs +++ b/framework/test/Volo.Abp.Auditing.Tests/Volo/Abp/Auditing/AbpAuditingTestModule.cs @@ -43,6 +43,12 @@ namespace Volo.Abp.Auditing "AppEntityWithSelector", type => type == typeof(AppEntityWithSelector)) ); + + options.EntityHistorySelectors.Add( + new NamedTypeSelector( + "AppEntityWithSoftDelete", + type => type == typeof(AppEntityWithSoftDelete)) + ); }); context.Services.AddType(); @@ -62,4 +68,4 @@ namespace Volo.Abp.Auditing return connection; } } -} \ No newline at end of file +} diff --git a/framework/test/Volo.Abp.Auditing.Tests/Volo/Abp/Auditing/App/Entities/AppEntityWithSoftDelete.cs b/framework/test/Volo.Abp.Auditing.Tests/Volo/Abp/Auditing/App/Entities/AppEntityWithSoftDelete.cs new file mode 100644 index 0000000000..0a81bb7f37 --- /dev/null +++ b/framework/test/Volo.Abp.Auditing.Tests/Volo/Abp/Auditing/App/Entities/AppEntityWithSoftDelete.cs @@ -0,0 +1,20 @@ +using System; +using Volo.Abp.Domain.Entities; + +namespace Volo.Abp.Auditing.App.Entities +{ + public class AppEntityWithSoftDelete : AggregateRoot, IHasDeletionTime + { + public AppEntityWithSoftDelete(Guid id, string name) + : base(id) + { + Name = name; + } + + public string Name { get; set; } + + public bool IsDeleted { get; set; } + + public DateTime? DeletionTime { get; set; } + } +} diff --git a/framework/test/Volo.Abp.Auditing.Tests/Volo/Abp/Auditing/App/EntityFrameworkCore/AbpAuditingTestDbContext.cs b/framework/test/Volo.Abp.Auditing.Tests/Volo/Abp/Auditing/App/EntityFrameworkCore/AbpAuditingTestDbContext.cs index d6b2d2b167..0cc69b68b6 100644 --- a/framework/test/Volo.Abp.Auditing.Tests/Volo/Abp/Auditing/App/EntityFrameworkCore/AbpAuditingTestDbContext.cs +++ b/framework/test/Volo.Abp.Auditing.Tests/Volo/Abp/Auditing/App/EntityFrameworkCore/AbpAuditingTestDbContext.cs @@ -17,15 +17,17 @@ namespace Volo.Abp.Auditing.App.EntityFrameworkCore public DbSet AppEntityWithPropertyHasAudited { get; set; } public DbSet AppEntityWithSelector { get; set; } - + public DbSet AppFullAuditedEntityWithAudited { get; set; } - + public DbSet AppEntityWithAuditedAndHasCustomAuditingProperties { get; set; } + public DbSet AppEntityWithSoftDelete { get; set; } + public AbpAuditingTestDbContext(DbContextOptions options) : base(options) { } } -} \ No newline at end of file +} diff --git a/framework/test/Volo.Abp.Auditing.Tests/Volo/Abp/Auditing/Auditing_Tests.cs b/framework/test/Volo.Abp.Auditing.Tests/Volo/Abp/Auditing/Auditing_Tests.cs index e72f67bbc3..e7fe5df8b4 100644 --- a/framework/test/Volo.Abp.Auditing.Tests/Volo/Abp/Auditing/Auditing_Tests.cs +++ b/framework/test/Volo.Abp.Auditing.Tests/Volo/Abp/Auditing/Auditing_Tests.cs @@ -314,5 +314,27 @@ namespace Volo.Abp.Auditing && x.EntityChanges[0].PropertyChanges[0].PropertyName == nameof(AppEntityWithAudited.Name))); #pragma warning restore 4014 } + + + [Fact] + public virtual async Task Should_Write_AuditLog_For_Soft_Deleted_Entity() + { + var entity = new AppEntityWithSoftDelete(Guid.NewGuid(), "test name"); + var repository = ServiceProvider.GetRequiredService>(); + await repository.InsertAsync(entity); + + using (var scope = _auditingManager.BeginScope()) + { + await repository.DeleteAsync(entity.Id); + await scope.SaveAsync(); + } + +#pragma warning disable 4014 + _auditingStore.Received().SaveAsync(Arg.Is(x => x.EntityChanges.Count == 1 && + x.EntityChanges[0].ChangeType == EntityChangeType.Deleted && + x.EntityChanges[0].PropertyChanges.Count == 0)); +#pragma warning restore 4014 + + } } }