FullAuditedEntity.DeleterId not set when deleted by host admin
When removing a FullAuditedEntity, IMultiTenant entity - that has a tenant - as the host admin - that does not have a tenant - the DeleterId property is not set and remains null.
I have not confirmed, but I would suspect this would be the case when deleting any entity that has a tenant different from the deleting user's.
Even manually setting the DeleterId and calling DbContext.SaveChanges does not work.
Could you please advise how to soft delete entities of different tenants as the host admin, while retaining the DeleterId?
hi @duard-sf
Look at the logic of the SetDeleterId method.
The data between the tenant and the tenant and the host is isolated.
https://github.com/abpframework/abp/blob/42f37c5ff01ad853a5425d15539d4222cd0dab69/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AuditPropertySetter.cs#L151-L181
From lines 158 - 161 I would assume that if I set the DeleterId manually , it would be saved as such in the database? Why is that not happening then?
Please show your code.
Here is what I initially tried:
HubRepository
public override async Task DeleteAsync(Hub entity, bool autoSave = false, CancellationToken cancellationToken = default)
{
entity.DeleterId = _currentUser.Id;
await base.DeleteAsync(entity, autoSave, cancellationToken);
}
I also looked at what base.DeleteAsync does, and implemented that manually, but it also did not work.
Where is the DbContext.SaveChanges call that you claim?
So this would be how I tried it the last time:
public override async Task DeleteAsync(Hub entity, bool autoSave = false, CancellationToken cancellationToken = default)
{
entity.DeleterId = _currentUser.Id;
DbSet.Remove(entity);
await DbContext.SaveChangesAsync(cancellationToken);
}
I believe it is because CancelDeletionForSoftDelete reloads the entity entry:
https://github.com/abpframework/abp/blob/6e0c55d4b67b0b9e52a95c2876067fc7bf95b175/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs#L256-L266
Try to override that to:
protected override void CancelDeletionForSoftDelete(EntityEntry entry)
{
if (!(entry.Entity is ISoftDelete))
{
return;
}
if (entry.Entity is IDeletionAuditedObject entity)
{
var deleterId = entity.DeleterId;
entry.Reload();
entry.Entity.As<IDeletionAuditedObject>().DeleterId = deleterId;
}
else
{
entry.Reload();
}
entry.State = EntityState.Modified;
entry.Entity.As<ISoftDelete>().IsDeleted = true;
}
That solved the issue for me, thanks!