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 d50e6b89f6..f87a05e4a9 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 @@ -198,6 +198,13 @@ public class EntityHistoryHelper : IEntityHistoryHelper, ITransientDependency { foreach (var (navigationEntry, index) in entityEntry.Navigations.Select((value, i) => ( value, i ))) { + var propertyInfo = navigationEntry.Metadata.PropertyInfo; + if (propertyInfo != null && + propertyInfo.IsDefined(typeof(DisableAuditingAttribute), true)) + { + continue; + } + if (AbpEfCoreNavigationHelper.IsNavigationEntryModified(entityEntry, index)) { var abpNavigationEntry = AbpEfCoreNavigationHelper.GetNavigationEntry(entityEntry, index); diff --git a/framework/test/Volo.Abp.Auditing.Tests/Volo/Abp/Auditing/App/Entities/AppEntityWithNavigationsAndDisableAuditing.cs b/framework/test/Volo.Abp.Auditing.Tests/Volo/Abp/Auditing/App/Entities/AppEntityWithNavigationsAndDisableAuditing.cs new file mode 100644 index 0000000000..573328387f --- /dev/null +++ b/framework/test/Volo.Abp.Auditing.Tests/Volo/Abp/Auditing/App/Entities/AppEntityWithNavigationsAndDisableAuditing.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using Volo.Abp.Domain.Entities; + +namespace Volo.Abp.Auditing.App.Entities; + +[Audited] +public class AppEntityWithNavigationsAndDisableAuditing : AggregateRoot +{ + protected AppEntityWithNavigationsAndDisableAuditing() + { + + } + + public AppEntityWithNavigationsAndDisableAuditing(Guid id, string name) + : base(id) + { + Name = name; + } + + public string Name { get; set; } + + [DisableAuditing] + public virtual List OneToMany { get; set; } +} + + +public class AppEntityWithNavigationsAndDisableAuditingChildOneToMany : Entity +{ + public Guid AppEntityWithNavigationsAndDisableAuditingId { get; set; } + + public string ChildName { 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 8aeeb2cfac..91698ea5ec 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 @@ -29,6 +29,8 @@ public class AbpAuditingTestDbContext : AbpDbContext public DbSet AppEntityWithNavigations { get; set; } public DbSet AppEntityWithNavigationChildOneToMany { get; set; } + public DbSet AppEntityWithNavigationsAndDisableAuditing { get; set; } + public AbpAuditingTestDbContext(DbContextOptions options) : base(options) { 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 aa11d7ffde..a88cf70292 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 @@ -1025,6 +1025,40 @@ public class Auditing_SaveEntityHistoryWhenNavigationChanges_Tests : AbpAuditing x.EntityChanges[0].PropertyChanges[0].PropertyName == nameof(AppEntityWithNavigationChildManyToMany.ChildName) && x.EntityChanges[0].PropertyChanges[0].PropertyTypeFullName == typeof(string).FullName)); +#pragma warning restore 4014 + } + + [Fact] + public virtual async Task Should_Not_Write_AuditLog_For_Navigation_Changes_With_DisableAuditing() + { + using (var scope = _auditingManager.BeginScope()) + { + var repository = ServiceProvider.GetRequiredService>(); + var entity = new AppEntityWithNavigationsAndDisableAuditing(Guid.NewGuid(), "test name"); + entity.OneToMany = new List + { + new AppEntityWithNavigationsAndDisableAuditingChildOneToMany + { + AppEntityWithNavigationsAndDisableAuditingId = entity.Id, + ChildName = "ChildName1" + } + }; + await repository.InsertAsync(entity); + await scope.SaveAsync(); + } + +#pragma warning disable 4014 + AuditingStore.Received().SaveAsync(Arg.Any()); +#pragma warning restore 4014 + +#pragma warning disable 4014 + AuditingStore.Received().SaveAsync(Arg.Is(x => x.EntityChanges.Count == 1 && + x.EntityChanges[0].ChangeType == EntityChangeType.Created && + x.EntityChanges[0].EntityTypeFullName == typeof(AppEntityWithNavigationsAndDisableAuditing).FullName && + x.EntityChanges[0].PropertyChanges.Count == 1 && + x.EntityChanges[0].PropertyChanges[0].PropertyName == nameof(AppEntityWithNavigationsAndDisableAuditing.Name) && + x.EntityChanges[0].PropertyChanges[0].PropertyTypeFullName == typeof(string).FullName)); + #pragma warning restore 4014 } }