From 8307a6f356614077e4ac3306c2a697fb6b31e391 Mon Sep 17 00:00:00 2001 From: maliming Date: Sat, 13 Apr 2024 10:16:31 +0800 Subject: [PATCH] Skip `PublishEntityDeletedEvent/PublishEntityUpdatedEvent` if only foreign keys have changed. --- .../Volo/Abp/EntityFrameworkCore/AbpDbContext.cs | 6 ++++++ .../Abp/TestApp/Testing/DomainEvents_Tests.cs | 15 +++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs index 65958ab5e5..caab5aff94 100644 --- a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs +++ b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs @@ -328,6 +328,12 @@ public abstract class AbpDbContext : DbContext, IAbpEfCoreDbContext, ApplyAbpConceptsForModifiedEntity(entry); if (entry.Properties.Any(x => x.IsModified && (x.Metadata.ValueGenerated == ValueGenerated.Never || x.Metadata.ValueGenerated == ValueGenerated.OnAdd))) { + if (entry.Properties.Where(x => x.IsModified).All(x => x.Metadata.IsForeignKey())) + { + // Skip `PublishEntityDeletedEvent/PublishEntityUpdatedEvent` if only foreign keys have changed. + break; + } + if (entry.Entity is ISoftDelete && entry.Entity.As().IsDeleted) { EntityChangeEventHelper.PublishEntityDeletedEvent(entry.Entity); diff --git a/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/DomainEvents_Tests.cs b/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/DomainEvents_Tests.cs index 4f82f26d95..f51ddf2b56 100644 --- a/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/DomainEvents_Tests.cs +++ b/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/DomainEvents_Tests.cs @@ -200,6 +200,11 @@ public abstract class DomainEvents_Tests : TestAppTestBase>(data => + { + throw new Exception("Should not trigger this event"); + }); + // Test with value object entityUpdatedEventTriggered = false; await WithUnitOfWorkAsync(async () => @@ -232,6 +237,11 @@ public abstract class DomainEvents_Tests : TestAppTestBase>(data => + { + throw new Exception("Should not trigger this event"); + }); + entityUpdatedEventTriggered = false; await WithUnitOfWorkAsync(async () => { @@ -258,6 +268,11 @@ public abstract class DomainEvents_Tests : TestAppTestBase>(data => + { + throw new Exception("Should not trigger this event"); + }); + entityUpdatedEventTriggered = false; await WithUnitOfWorkAsync(async () => {