From 88bfa816bc8db036d162951439579f2576dea3c6 Mon Sep 17 00:00:00 2001 From: maliming Date: Tue, 4 Nov 2025 17:04:21 +0800 Subject: [PATCH 1/6] Improve foreign key change detection in domain events https://abp.io/support/questions/10057 --- .../Abp/EntityFrameworkCore/AbpDbContext.cs | 7 +-- .../AbpAuditingTestDbContext.cs | 2 +- .../AbpEntityFrameworkCoreTestModule.cs | 5 ++ .../DomainEvents/DomainEvents_Tests.cs | 50 +++++++++++++++++++ .../TestMigrationsDbContext.cs | 8 ++- .../EntityFrameworkCore/TestAppDbContext.cs | 8 ++- .../Domain/AppEntityWithNavigations.cs | 13 +++++ .../Abp/TestApp/Testing/DomainEvents_Tests.cs | 42 +++++++++++++++- 8 files changed, 127 insertions(+), 8 deletions(-) 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 e91d08b8c9..09eea8aecf 100644 --- a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs +++ b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs @@ -268,7 +268,7 @@ public abstract class AbpDbContext : DbContext, IAbpEfCoreDbContext, if (EntityChangeOptions.Value.PublishEntityUpdatedEventWhenNavigationChanges) { var ignoredEntity = EntityChangeOptions.Value.IgnoredNavigationEntitySelectors.Any(selector => selector.Predicate(entityEntry.Entity.GetType())); - var onlyForeignKeyModifiedEntity = entityEntry.State == EntityState.Modified && entityEntry.Properties.Where(x => x.IsModified).All(x => x.Metadata.IsForeignKey()); + var onlyForeignKeyModifiedEntity = entityEntry.State == EntityState.Modified && entityEntry.Properties.Where(x => x.IsModified).All(x => x.Metadata.IsForeignKey() && (x.CurrentValue == null || x.OriginalValue?.ToString() == x.CurrentValue?.ToString())); if ((entityEntry.State == EntityState.Unchanged && ignoredEntity) || onlyForeignKeyModifiedEntity && ignoredEntity) { continue; @@ -292,7 +292,7 @@ public abstract class AbpDbContext : DbContext, IAbpEfCoreDbContext, } else if (entityEntry.Properties.Any(x => x.IsModified && (x.Metadata.ValueGenerated == ValueGenerated.Never || x.Metadata.ValueGenerated == ValueGenerated.OnAdd))) { - if (entityEntry.Properties.Where(x => x.IsModified).All(x => x.Metadata.IsForeignKey())) + if (entityEntry.Properties.Where(x => x.IsModified).All(x => x.Metadata.IsForeignKey() && (x.CurrentValue == null || x.OriginalValue?.ToString() == x.CurrentValue?.ToString()))) { // Skip `PublishEntityDeletedEvent/PublishEntityUpdatedEvent` if only foreign keys have changed. break; @@ -428,7 +428,8 @@ public abstract class AbpDbContext : DbContext, IAbpEfCoreDbContext, case EntityState.Modified: 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())) + var modifiedProperties = entry.Properties.Where(x => x.IsModified).ToList(); + if (modifiedProperties.All(x => x.Metadata.IsForeignKey() && (x.CurrentValue == null || x.OriginalValue?.ToString() == x.CurrentValue?.ToString()))) { // Skip `PublishEntityDeletedEvent/PublishEntityUpdatedEvent` if only foreign keys have changed. break; 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 b102396cb0..8aeeb2cfac 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 @@ -28,7 +28,7 @@ public class AbpAuditingTestDbContext : AbpDbContext public DbSet AppEntityWithValueObject { get; set; } public DbSet AppEntityWithNavigations { get; set; } - + public DbSet AppEntityWithNavigationChildOneToMany { get; set; } public AbpAuditingTestDbContext(DbContextOptions options) : base(options) { diff --git a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/AbpEntityFrameworkCoreTestModule.cs b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/AbpEntityFrameworkCoreTestModule.cs index 22c0b92477..5f905ca062 100644 --- a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/AbpEntityFrameworkCoreTestModule.cs +++ b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/AbpEntityFrameworkCoreTestModule.cs @@ -58,6 +58,11 @@ public class AbpEntityFrameworkCoreTestModule : AbpModule { opt.DefaultWithDetailsFunc = q => q.Include(p => p.BlogPosts); }); + + options.Entity(opt => + { + opt.DefaultWithDetailsFunc = q => q.Include(p => p.OneToMany); + }); }); context.Services.AddAbpDbContext(options => diff --git a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DomainEvents/DomainEvents_Tests.cs b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DomainEvents/DomainEvents_Tests.cs index 6249d588f5..2967e15f16 100644 --- a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DomainEvents/DomainEvents_Tests.cs +++ b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DomainEvents/DomainEvents_Tests.cs @@ -48,6 +48,7 @@ public class AbpEntityChangeOptions_DomainEvents_IgnoreEntityChangeSelectorList_ public class AbpEfCoreDomainEvents_Tests : EntityFrameworkCoreTestBase { protected readonly IRepository AppEntityWithNavigationsRepository; + protected readonly IRepository AppEntityWithNavigationChildOneToManyRepository; protected readonly ILocalEventBus LocalEventBus; protected readonly IRepository PersonRepository; protected bool _loadEntityWithoutDetails = false; @@ -55,6 +56,7 @@ public class AbpEfCoreDomainEvents_Tests : EntityFrameworkCoreTestBase public AbpEfCoreDomainEvents_Tests() { AppEntityWithNavigationsRepository = GetRequiredService>(); + AppEntityWithNavigationChildOneToManyRepository = GetRequiredService>(); LocalEventBus = GetRequiredService(); PersonRepository = GetRequiredService>(); } @@ -357,6 +359,22 @@ public class AbpEfCoreDomainEvents_Tests : EntityFrameworkCoreTestBase var entityId = Guid.NewGuid(); await AppEntityWithNavigationsRepository.InsertAsync(new AppEntityWithNavigations(entityId, "TestEntity") + { + OneToMany = new List() + { + new AppEntityWithNavigationChildOneToMany(Guid.NewGuid()) + { + ChildName = "ChildName1" + }, + new AppEntityWithNavigationChildOneToMany(Guid.NewGuid()) + { + ChildName = "ChildName2" + } + } + }); + + var entityId2 = Guid.NewGuid(); + await AppEntityWithNavigationsRepository.InsertAsync(new AppEntityWithNavigations(entityId2, "TestEntity") { OneToMany = new List() { @@ -367,6 +385,33 @@ public class AbpEfCoreDomainEvents_Tests : EntityFrameworkCoreTestBase } }); + var oneToManyEntity = Guid.NewGuid(); + await AppEntityWithNavigationChildOneToManyRepository.InsertAsync( + new AppEntityWithNavigationChildOneToMany(oneToManyEntity) + { + AppEntityWithNavigationId = entityId, + }); + + LocalEventBus.Subscribe>(data => + { + data.Entity.AppEntityWithNavigationId.ShouldBe(entityId2); + return Task.CompletedTask; + }); + + using (var scope = ServiceProvider.CreateScope()) + { + var uowManager = scope.ServiceProvider.GetRequiredService(); + using (var uow = uowManager.Begin()) + { + var entity = await AppEntityWithNavigationChildOneToManyRepository.GetAsync(oneToManyEntity); + + entity.AppEntityWithNavigationId = entityId2; + await AppEntityWithNavigationChildOneToManyRepository.UpdateAsync(entity); + + await uow.CompleteAsync(); + } + } + var entityUpdatedEventTriggered = false; LocalEventBus.Subscribe>(data => @@ -375,6 +420,11 @@ public class AbpEfCoreDomainEvents_Tests : EntityFrameworkCoreTestBase return Task.CompletedTask; }); + LocalEventBus.Subscribe>(data => + { + throw new Exception("Should not trigger this event"); + }); + using (var scope = ServiceProvider.CreateScope()) { var uowManager = scope.ServiceProvider.GetRequiredService(); diff --git a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/TestMigrationsDbContext.cs b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/TestMigrationsDbContext.cs index d05e367ff7..8c537b2b73 100644 --- a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/TestMigrationsDbContext.cs +++ b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/TestMigrationsDbContext.cs @@ -27,6 +27,7 @@ public class TestMigrationsDbContext : AbpDbContext public DbSet Categories { get; set; } public DbSet AppEntityWithNavigations { get; set; } + public DbSet AppEntityWithNavigationChildOneToMany { get; set; } public DbSet AppEntityWithNavigationsForeign { get; set; } @@ -81,7 +82,12 @@ public class TestMigrationsDbContext : AbpDbContext b.HasOne(x => x.OneToOne).WithOne().HasForeignKey(x => x.Id); b.HasMany(x => x.OneToMany).WithOne().HasForeignKey(x => x.AppEntityWithNavigationId); b.HasMany(x => x.ManyToMany).WithMany(x => x.ManyToMany).UsingEntity(); - b.HasOne().WithMany().HasForeignKey(x => x.AppEntityWithNavigationForeignId).IsRequired(false); + }); + + modelBuilder.Entity(b => + { + b.ConfigureByConvention(); + b.HasMany(x => x.OneToMany).WithOne().HasForeignKey(x => x.AppEntityWithNavigationForeignId); }); modelBuilder.Entity(b => diff --git a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/TestApp/EntityFrameworkCore/TestAppDbContext.cs b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/TestApp/EntityFrameworkCore/TestAppDbContext.cs index 242809bf7c..7f17346bf5 100644 --- a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/TestApp/EntityFrameworkCore/TestAppDbContext.cs +++ b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/TestApp/EntityFrameworkCore/TestAppDbContext.cs @@ -34,6 +34,7 @@ public class TestAppDbContext : AbpDbContext, IThirdDbContext, public DbSet Categories { get; set; } public DbSet AppEntityWithNavigations { get; set; } + public DbSet AppEntityWithNavigationChildOneToMany { get; set; } public DbSet AppEntityWithNavigationsForeign { get; set; } @@ -107,7 +108,12 @@ public class TestAppDbContext : AbpDbContext, IThirdDbContext, b.HasOne(x => x.OneToOne).WithOne().HasForeignKey(x => x.Id); b.HasMany(x => x.OneToMany).WithOne().HasForeignKey(x => x.AppEntityWithNavigationId); b.HasMany(x => x.ManyToMany).WithMany(x => x.ManyToMany).UsingEntity(); - b.HasOne().WithMany().HasForeignKey(x => x.AppEntityWithNavigationForeignId).IsRequired(false); + }); + + modelBuilder.Entity(b => + { + b.ConfigureByConvention(); + b.HasMany(x => x.OneToMany).WithOne().HasForeignKey(x => x.AppEntityWithNavigationForeignId); }); modelBuilder.Entity(b => diff --git a/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Domain/AppEntityWithNavigations.cs b/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Domain/AppEntityWithNavigations.cs index e7815b7622..c3ab4466a6 100644 --- a/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Domain/AppEntityWithNavigations.cs +++ b/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Domain/AppEntityWithNavigations.cs @@ -65,6 +65,17 @@ public class AppEntityWithNavigationChildOneToOneAndOneToOne : Entity public class AppEntityWithNavigationChildOneToMany : Entity { + public AppEntityWithNavigationChildOneToMany() + { + + } + + public AppEntityWithNavigationChildOneToMany(Guid id) + : base(id) + { + + } + public Guid AppEntityWithNavigationId { get; set; } public string ChildName { get; set; } @@ -107,4 +118,6 @@ public class AppEntityWithNavigationsForeign : AggregateRoot } public string Name { get; set; } + + public virtual List OneToMany { get; set; } } 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 032d714390..4ac68dd718 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 @@ -347,10 +347,20 @@ public abstract class AbpEntityChangeOptions_DomainEvents_Tests var entityWithNavigationForeignId = Guid.NewGuid(); var entityWithNavigationForeignId2 = Guid.NewGuid(); + var entityWithNavigationForeignId3 = Guid.NewGuid(); await AppEntityWithNavigationForeignRepository.InsertAsync(new AppEntityWithNavigationsForeign(entityWithNavigationForeignId, "TestEntityWithNavigationForeign")); await AppEntityWithNavigationForeignRepository.InsertAsync(new AppEntityWithNavigationsForeign(entityWithNavigationForeignId2, "TestEntityWithNavigationForeign2")); + await AppEntityWithNavigationForeignRepository.InsertAsync(new AppEntityWithNavigationsForeign(entityWithNavigationForeignId3, "TestEntityWithNavigationForeign3") + { + OneToMany = new List() + { + new AppEntityWithNavigations(Guid.NewGuid(), "TestEntity2"), + new AppEntityWithNavigations(Guid.NewGuid(), "TestEntity3") + } + }); var entityUpdatedEventTriggered = false; + var entityWithNavigationsForeignUpdatedEventTriggered = false; LocalEventBus.Subscribe>(data => { @@ -358,6 +368,12 @@ public abstract class AbpEntityChangeOptions_DomainEvents_Tests return Task.CompletedTask; }); + LocalEventBus.Subscribe>(data => + { + entityWithNavigationsForeignUpdatedEventTriggered = !entityWithNavigationsForeignUpdatedEventTriggered; + return Task.CompletedTask; + }); + // Test with simple property with foreign key await WithUnitOfWorkAsync(async () => { @@ -368,17 +384,39 @@ public abstract class AbpEntityChangeOptions_DomainEvents_Tests }); entityUpdatedEventTriggered.ShouldBeTrue(); - // Test only foreign key changed + // Test only foreign key changed to null entityUpdatedEventTriggered = false; await WithUnitOfWorkAsync(async () => { var entity = await AppEntityWithNavigationsRepository.GetAsync(entityId); - entity.AppEntityWithNavigationForeignId = entityWithNavigationForeignId2; + entity.AppEntityWithNavigationForeignId = null; await AppEntityWithNavigationsRepository.UpdateAsync(entity); }); entityUpdatedEventTriggered.ShouldBeFalse(); + // Test only foreign key change to new id + entityUpdatedEventTriggered = false; + await WithUnitOfWorkAsync(async () => + { + var entity = await AppEntityWithNavigationsRepository.GetAsync(entityId); + entity.AppEntityWithNavigationForeignId = entityWithNavigationForeignId; + await AppEntityWithNavigationsRepository.UpdateAsync(entity); + }); + entityUpdatedEventTriggered.ShouldBeTrue(); + + // Test only foreign key changed + entityWithNavigationsForeignUpdatedEventTriggered = false; + await WithUnitOfWorkAsync(async () => + { + var entity = await AppEntityWithNavigationForeignRepository.GetAsync(entityWithNavigationForeignId3); + entity.OneToMany.ShouldNotBeEmpty(); + entity.OneToMany.Clear(); + await AppEntityWithNavigationForeignRepository.UpdateAsync(entity); + }); + entityWithNavigationsForeignUpdatedEventTriggered.ShouldBeFalse(); + // Test with simple property with value object + entityUpdatedEventTriggered = false; await WithUnitOfWorkAsync(async () => { var entity = await AppEntityWithNavigationsRepository.GetAsync(entityId); From 9aaa74c962bfe36008ceeb34b70a9758b1270bc7 Mon Sep 17 00:00:00 2001 From: maliming Date: Tue, 4 Nov 2025 17:17:54 +0800 Subject: [PATCH 2/6] Refactor foreign key modification checks in AbpDbContext --- .../Volo/Abp/EntityFrameworkCore/AbpDbContext.cs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) 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 09eea8aecf..b52941ced5 100644 --- a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs +++ b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs @@ -268,7 +268,7 @@ public abstract class AbpDbContext : DbContext, IAbpEfCoreDbContext, if (EntityChangeOptions.Value.PublishEntityUpdatedEventWhenNavigationChanges) { var ignoredEntity = EntityChangeOptions.Value.IgnoredNavigationEntitySelectors.Any(selector => selector.Predicate(entityEntry.Entity.GetType())); - var onlyForeignKeyModifiedEntity = entityEntry.State == EntityState.Modified && entityEntry.Properties.Where(x => x.IsModified).All(x => x.Metadata.IsForeignKey() && (x.CurrentValue == null || x.OriginalValue?.ToString() == x.CurrentValue?.ToString())); + var onlyForeignKeyModifiedEntity = entityEntry.State == EntityState.Modified && IsOnlyForeignKeysDeleted(entityEntry); if ((entityEntry.State == EntityState.Unchanged && ignoredEntity) || onlyForeignKeyModifiedEntity && ignoredEntity) { continue; @@ -292,7 +292,7 @@ public abstract class AbpDbContext : DbContext, IAbpEfCoreDbContext, } else if (entityEntry.Properties.Any(x => x.IsModified && (x.Metadata.ValueGenerated == ValueGenerated.Never || x.Metadata.ValueGenerated == ValueGenerated.OnAdd))) { - if (entityEntry.Properties.Where(x => x.IsModified).All(x => x.Metadata.IsForeignKey() && (x.CurrentValue == null || x.OriginalValue?.ToString() == x.CurrentValue?.ToString()))) + if (IsOnlyForeignKeysDeleted(entityEntry)) { // Skip `PublishEntityDeletedEvent/PublishEntityUpdatedEvent` if only foreign keys have changed. break; @@ -428,8 +428,7 @@ public abstract class AbpDbContext : DbContext, IAbpEfCoreDbContext, case EntityState.Modified: if (entry.Properties.Any(x => x.IsModified && (x.Metadata.ValueGenerated == ValueGenerated.Never || x.Metadata.ValueGenerated == ValueGenerated.OnAdd))) { - var modifiedProperties = entry.Properties.Where(x => x.IsModified).ToList(); - if (modifiedProperties.All(x => x.Metadata.IsForeignKey() && (x.CurrentValue == null || x.OriginalValue?.ToString() == x.CurrentValue?.ToString()))) + if (IsOnlyForeignKeysDeleted(entry)) { // Skip `PublishEntityDeletedEvent/PublishEntityUpdatedEvent` if only foreign keys have changed. break; @@ -473,6 +472,12 @@ public abstract class AbpDbContext : DbContext, IAbpEfCoreDbContext, } } + protected virtual bool IsOnlyForeignKeysDeleted(EntityEntry entry) + { + return entry.Properties.All(x => x.IsModified && x.Metadata.IsForeignKey() && + (x.CurrentValue == null || x.OriginalValue?.ToString() == x.CurrentValue?.ToString())); + } + protected virtual void HandlePropertiesBeforeSave() { var entries = ChangeTracker.Entries().ToList(); From 19df6aacd1ee8406fc077619b8f91810dc665d1b Mon Sep 17 00:00:00 2001 From: maliming Date: Tue, 4 Nov 2025 17:24:46 +0800 Subject: [PATCH 3/6] Rename IsOnlyForeignKeysDeleted to IsOnlyForeignKeysModified --- .../Volo/Abp/EntityFrameworkCore/AbpDbContext.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 b52941ced5..ccdaf4bd7d 100644 --- a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs +++ b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs @@ -472,7 +472,7 @@ public abstract class AbpDbContext : DbContext, IAbpEfCoreDbContext, } } - protected virtual bool IsOnlyForeignKeysDeleted(EntityEntry entry) + protected virtual bool IsOnlyForeignKeysModified(EntityEntry entry) { return entry.Properties.All(x => x.IsModified && x.Metadata.IsForeignKey() && (x.CurrentValue == null || x.OriginalValue?.ToString() == x.CurrentValue?.ToString())); From 638ca61c9629f619056e422a00ea4477b1a58644 Mon Sep 17 00:00:00 2001 From: maliming Date: Tue, 4 Nov 2025 17:28:24 +0800 Subject: [PATCH 4/6] Rename IsOnlyForeignKeysDeleted to IsOnlyForeignKeysModified --- .../Volo/Abp/EntityFrameworkCore/AbpDbContext.cs | 6 +++--- .../Volo/Abp/TestApp/Testing/DomainEvents_Tests.cs | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) 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 ccdaf4bd7d..4cfa949a50 100644 --- a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs +++ b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs @@ -268,7 +268,7 @@ public abstract class AbpDbContext : DbContext, IAbpEfCoreDbContext, if (EntityChangeOptions.Value.PublishEntityUpdatedEventWhenNavigationChanges) { var ignoredEntity = EntityChangeOptions.Value.IgnoredNavigationEntitySelectors.Any(selector => selector.Predicate(entityEntry.Entity.GetType())); - var onlyForeignKeyModifiedEntity = entityEntry.State == EntityState.Modified && IsOnlyForeignKeysDeleted(entityEntry); + var onlyForeignKeyModifiedEntity = entityEntry.State == EntityState.Modified && IsOnlyForeignKeysModified(entityEntry); if ((entityEntry.State == EntityState.Unchanged && ignoredEntity) || onlyForeignKeyModifiedEntity && ignoredEntity) { continue; @@ -292,7 +292,7 @@ public abstract class AbpDbContext : DbContext, IAbpEfCoreDbContext, } else if (entityEntry.Properties.Any(x => x.IsModified && (x.Metadata.ValueGenerated == ValueGenerated.Never || x.Metadata.ValueGenerated == ValueGenerated.OnAdd))) { - if (IsOnlyForeignKeysDeleted(entityEntry)) + if (IsOnlyForeignKeysModified(entityEntry)) { // Skip `PublishEntityDeletedEvent/PublishEntityUpdatedEvent` if only foreign keys have changed. break; @@ -428,7 +428,7 @@ public abstract class AbpDbContext : DbContext, IAbpEfCoreDbContext, case EntityState.Modified: if (entry.Properties.Any(x => x.IsModified && (x.Metadata.ValueGenerated == ValueGenerated.Never || x.Metadata.ValueGenerated == ValueGenerated.OnAdd))) { - if (IsOnlyForeignKeysDeleted(entry)) + if (IsOnlyForeignKeysModified(entry)) { // Skip `PublishEntityDeletedEvent/PublishEntityUpdatedEvent` if only foreign keys have changed. break; 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 4ac68dd718..58010449ff 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 @@ -384,7 +384,7 @@ public abstract class AbpEntityChangeOptions_DomainEvents_Tests }); entityUpdatedEventTriggered.ShouldBeTrue(); - // Test only foreign key changed to null + // Test only foreign key change to null entityUpdatedEventTriggered = false; await WithUnitOfWorkAsync(async () => { From ba7c545e1f3cb6d878bdaf8cb8f672ccd77bf679 Mon Sep 17 00:00:00 2001 From: Ma Liming Date: Tue, 4 Nov 2025 17:40:14 +0800 Subject: [PATCH 5/6] Update build-and-test.yml --- .github/workflows/build-and-test.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.github/workflows/build-and-test.yml b/.github/workflows/build-and-test.yml index 4ecb0c6543..b31eff690a 100644 --- a/.github/workflows/build-and-test.yml +++ b/.github/workflows/build-and-test.yml @@ -51,11 +51,14 @@ jobs: timeout-minutes: 50 if: ${{ !github.event.pull_request.draft }} steps: + - uses: jlumbroso/free-disk-space@main + - uses: PSModule/install-powershell@v1 + with: + Version: latest - uses: actions/checkout@v2 - uses: actions/setup-dotnet@master with: dotnet-version: 9.0.100 - - name: Build All run: ./build-all.ps1 working-directory: ./build From 71d71dfbd9c92f993fbe6f86d3c866941d1298e4 Mon Sep 17 00:00:00 2001 From: maliming Date: Tue, 4 Nov 2025 19:40:42 +0800 Subject: [PATCH 6/6] Fix IsOnlyForeignKeysModified logic in AbpDbContext --- .../Volo/Abp/EntityFrameworkCore/AbpDbContext.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 4cfa949a50..7fa051dd53 100644 --- a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs +++ b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs @@ -474,7 +474,7 @@ public abstract class AbpDbContext : DbContext, IAbpEfCoreDbContext, protected virtual bool IsOnlyForeignKeysModified(EntityEntry entry) { - return entry.Properties.All(x => x.IsModified && x.Metadata.IsForeignKey() && + return entry.Properties.Where(x => x.IsModified).All(x => x.Metadata.IsForeignKey() && (x.CurrentValue == null || x.OriginalValue?.ToString() == x.CurrentValue?.ToString())); }