Do not create PropertyChanges for soft deleted entities.

pull/9735/head
maliming 4 years ago
parent 3c296184d0
commit 566e41924a

@ -149,6 +149,8 @@ namespace Volo.Abp.EntityFrameworkCore
{
try
{
var changeReport = ApplyAbpConcepts();
var auditLog = AuditingManager?.Current?.Log;
List<EntityChangeInfo> 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);

@ -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
}
}
}

Loading…
Cancel
Save