Open Closed

Audit log entity changes for owned entities #4051


User avatar
0
s.beckers created
  • ABP Framework version: v5.2.2
  • UI type: Angular
  • DB provider: EF Core
  • Tiered (MVC) or Identity Server Separated (Angular): no

Is there any way to track entity changes of an owned entity type? https://learn.microsoft.com/en-us/ef/core/modeling/owned-entities

Currently we don't get PropertyChanges in the following scenario:

Entities:

public class Address
{
    public string City { get; set; }
    public string PostalCode { get; set; }
    public string StreetAndNumber { get; set; }
    ...
}

[Audited]
public class Company : FullAuditedAggregateRoot<Guid>, IMultiTenant
{
    public string Name { get; set; }
    public Address Address { get; set; }
    ...
}

EFCore DbContext config:

public static void ConfigureModule(this ModelBuilder builder)
{
    Check.NotNull(builder, nameof(builder));

    builder.Entity<Company>(b =>
    {
        b.ToTable(ModuleDbProperties.DbTablePrefix + "Companies", ModuleDbProperties.DbSchema);
        b.ConfigureByConvention();
        b.Property(x => x.Name).IsRequired().HasMaxLength(CompanyConsts.NameMaxLength);
        b.OwnsOne(
            x => x.Address,
            b =>
            {
                b.Property(x => x.City).HasMaxLength(AddressConsts.CityMaxLength);
                b.Property(x => x.PostalCode).HasMaxLength(AddressConsts.PostalCodeMaxLength);
                b.Property(x => x.StreetAndNumber).HasMaxLength(AddressConsts.StreetAndNumberMaxLength);
            });
        ...
    });
}
AuditLog auditLog = await _auditLogRepository.GetAsync(...);
EntityChange entityChange = auditLog.EntityChanges.First();
entityChange.PropertyChanges

When updating the company's name and address details, only a property change for the name is generated, not for the address properties. Do we miss some configuration here or is it not supported (yet)? How can we get property changes for the address information?

Thanks in advance!


4 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    I will confirm that.

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    I checked. This case is not supported.

  • User Avatar
    1
    s.beckers created

    Hi

    Thanks for confirming. I've overridden the EntityHistoryHelper's GetPropertyChanges method to include the property changes of the referenced entities.

    using Microsoft.EntityFrameworkCore.ChangeTracking;
    using Microsoft.Extensions.Options;
    using System.Collections.Generic;
    using Volo.Abp.Auditing;
    using Volo.Abp.DependencyInjection;
    using Volo.Abp.EntityFrameworkCore.EntityHistory;
    using Volo.Abp.Json;
    using Volo.Abp.Timing;
    
    namespace MyCompany.MyModule;
    
    [Dependency(ReplaceServices = true)]
    public class CustomEntityHistoryHelper : EntityHistoryHelper
    {
        public CustomEntityHistoryHelper(
            IAuditingStore auditingStore,
            IOptions<AbpAuditingOptions> options,
            IClock clock,
            IJsonSerializer jsonSerializer,
            IAuditingHelper auditingHelper)
            : base(auditingStore, options, clock, jsonSerializer, auditingHelper)
        {
        }
    
        protected override List<EntityPropertyChangeInfo> GetPropertyChanges(EntityEntry entityEntry)
        {
            List<EntityPropertyChangeInfo> propertyChanges = base.GetPropertyChanges(entityEntry);
    
            foreach (ReferenceEntry reference in entityEntry.References)
            {
                if (reference.TargetEntry == null)
                {
                    continue;
                }
    
                List<EntityPropertyChangeInfo> referencePropertyChanges = GetPropertyChanges(reference.TargetEntry);
    
                foreach (EntityPropertyChangeInfo referencePropertyChange in referencePropertyChanges)
                {
                    referencePropertyChange.PropertyName = $"{reference.Metadata.Name}.{referencePropertyChange.PropertyName}";
                }
    
                propertyChanges.AddRange(referencePropertyChanges);
            }
    
            return propertyChanges;
        }
    }
    

    Any remarks on this approach?

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    You can check that the entity type is Company because there may be more complex entities.

Made with ❤️ on ABP v8.2.0-preview Updated on March 25, 2024, 15:11