Browse Source
Merge pull request #9735 from abpframework/maliming/EntityChangeType
Do not create PropertyChanges for soft deleted entities.
pull/10068/head
Halil İbrahim Kalkan
4 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with
63 additions and
13 deletions
-
framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs
-
framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/EntityHistory/EntityHistoryHelper.cs
-
framework/test/Volo.Abp.Auditing.Tests/Volo/Abp/Auditing/AbpAuditingTestModule.cs
-
framework/test/Volo.Abp.Auditing.Tests/Volo/Abp/Auditing/App/Entities/AppEntityWithSoftDelete.cs
-
framework/test/Volo.Abp.Auditing.Tests/Volo/Abp/Auditing/App/EntityFrameworkCore/AbpAuditingTestDbContext.cs
-
framework/test/Volo.Abp.Auditing.Tests/Volo/Abp/Auditing/Auditing_Tests.cs
|
|
|
@ -155,6 +155,10 @@ namespace Volo.Abp.EntityFrameworkCore |
|
|
|
{ |
|
|
|
try |
|
|
|
{ |
|
|
|
ApplyAbpConcepts(); |
|
|
|
|
|
|
|
var eventReport = CreateEventReport(); |
|
|
|
|
|
|
|
var auditLog = AuditingManager?.Current?.Log; |
|
|
|
List<EntityChangeInfo> entityChangeList = null; |
|
|
|
if (auditLog != null) |
|
|
|
@ -162,10 +166,6 @@ namespace Volo.Abp.EntityFrameworkCore |
|
|
|
entityChangeList = EntityHistoryHelper.CreateChangeList(ChangeTracker.Entries().ToList()); |
|
|
|
} |
|
|
|
|
|
|
|
ApplyAbpConcepts(); |
|
|
|
|
|
|
|
var eventReport = CreateEventReport(); |
|
|
|
|
|
|
|
var result = await base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken); |
|
|
|
|
|
|
|
PublishEntityEvents(eventReport); |
|
|
|
|
|
|
|
@ -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<ISoftDelete>().IsDeleted; |
|
|
|
} |
|
|
|
|
|
|
|
@ -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<Auditing_Tests.MyAuditedObject1>(); |
|
|
|
@ -62,4 +68,4 @@ namespace Volo.Abp.Auditing |
|
|
|
return connection; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@ -0,0 +1,20 @@ |
|
|
|
using System; |
|
|
|
using Volo.Abp.Domain.Entities; |
|
|
|
|
|
|
|
namespace Volo.Abp.Auditing.App.Entities |
|
|
|
{ |
|
|
|
public class AppEntityWithSoftDelete : AggregateRoot<Guid>, 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; } |
|
|
|
} |
|
|
|
} |
|
|
|
@ -17,15 +17,17 @@ namespace Volo.Abp.Auditing.App.EntityFrameworkCore |
|
|
|
public DbSet<AppEntityWithPropertyHasAudited> AppEntityWithPropertyHasAudited { get; set; } |
|
|
|
|
|
|
|
public DbSet<AppEntityWithSelector> AppEntityWithSelector { get; set; } |
|
|
|
|
|
|
|
|
|
|
|
public DbSet<AppFullAuditedEntityWithAudited> AppFullAuditedEntityWithAudited { get; set; } |
|
|
|
|
|
|
|
|
|
|
|
public DbSet<AppEntityWithAuditedAndHasCustomAuditingProperties> AppEntityWithAuditedAndHasCustomAuditingProperties { get; set; } |
|
|
|
|
|
|
|
public DbSet<AppEntityWithSoftDelete> AppEntityWithSoftDelete { get; set; } |
|
|
|
|
|
|
|
public AbpAuditingTestDbContext(DbContextOptions<AbpAuditingTestDbContext> options) |
|
|
|
: base(options) |
|
|
|
{ |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@ -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<IBasicRepository<AppEntityWithSoftDelete, Guid>>(); |
|
|
|
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<AuditLogInfo>(x => x.EntityChanges.Count == 1 && |
|
|
|
x.EntityChanges[0].ChangeType == EntityChangeType.Deleted && |
|
|
|
x.EntityChanges[0].PropertyChanges.Count == 0)); |
|
|
|
#pragma warning restore 4014
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|