From 078643680c22d9e2a23be500b2871c25d1274926 Mon Sep 17 00:00:00 2001 From: maliming Date: Thu, 5 Sep 2024 14:27:01 +0800 Subject: [PATCH] Backup `ExtraProperties` during `Reload` entry. Because `Reload` will not convert the `ExtraProperties` from the JSON string to the `Dictionary`. --- .../Abp/EntityFrameworkCore/AbpDbContext.cs | 12 ++++++++++ .../Domain/ExtraProperties_Tests.cs | 22 +++++++++++++++++++ .../Volo/Abp/TestApp/Domain/City.cs | 4 +++- .../TestApp/Testing/ConcurrencyStamp_Tests.cs | 3 ++- 4 files changed, 39 insertions(+), 2 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 c4bd75cca2f..c471d89acf8 100644 --- a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs +++ b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs @@ -614,7 +614,19 @@ protected virtual void ApplyAbpConceptsForDeletedEntity(EntityEntry entry) return; } + ExtraPropertyDictionary? originalExtraProperties = null; + if (entry.Entity is IHasExtraProperties) + { + originalExtraProperties = entry.OriginalValues.GetValue(nameof(IHasExtraProperties.ExtraProperties)); + } + entry.Reload(); + + if (entry.Entity is IHasExtraProperties) + { + ObjectHelper.TrySetProperty(entry.Entity.As(), x => x.ExtraProperties, () => originalExtraProperties); + } + ObjectHelper.TrySetProperty(entry.Entity.As(), x => x.IsDeleted, () => true); SetDeletionAuditProperties(entry); } diff --git a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Domain/ExtraProperties_Tests.cs b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Domain/ExtraProperties_Tests.cs index 42b6ca2a679..59eaa367fab 100644 --- a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Domain/ExtraProperties_Tests.cs +++ b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Domain/ExtraProperties_Tests.cs @@ -1,6 +1,7 @@ using System; using System.Linq; using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; using Shouldly; using Volo.Abp.Data; using Volo.Abp.Domain.Repositories; @@ -59,6 +60,27 @@ await WithUnitOfWorkAsync(async () => }); } + [Fact] + public async Task Should_Get_An_Extra_Property_After_Soft_Deletion() + { + var london = await CityRepository.FindByNameAsync("London"); + london.HasProperty("PhoneCode").ShouldBeTrue(); + london.GetProperty("PhoneCode").ShouldBe("42"); + + await CityRepository.DeleteAsync(london); + + london = await CityRepository.FindByNameAsync("London"); + london.ShouldBeNull(); + + using (var uow = ServiceProvider.GetRequiredService().Disable()) + { + london = await CityRepository.FindByNameAsync("London"); + london.IsDeleted.ShouldBeTrue(); + london.HasProperty("PhoneCode").ShouldBeTrue(); + london.GetProperty("PhoneCode").ShouldBe("42"); + } + } + public enum Color { Red = 0, diff --git a/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Domain/City.cs b/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Domain/City.cs index 15ca147d911..50c3d32458e 100644 --- a/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Domain/City.cs +++ b/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Domain/City.cs @@ -5,12 +5,14 @@ namespace Volo.Abp.TestApp.Domain; -public class City : AggregateRoot +public class City : AggregateRoot, ISoftDelete { public string Name { get; set; } public ICollection Districts { get; set; } + public bool IsDeleted { get; set; } + private City() { diff --git a/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/ConcurrencyStamp_Tests.cs b/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/ConcurrencyStamp_Tests.cs index 2f9f8c12575..2906431ff1d 100644 --- a/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/ConcurrencyStamp_Tests.cs +++ b/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/ConcurrencyStamp_Tests.cs @@ -1,5 +1,6 @@ using System.Threading.Tasks; using Volo.Abp.Data; +using Volo.Abp.Domain.Repositories; using Volo.Abp.Modularity; using Volo.Abp.TestApp.Domain; using Xunit; @@ -49,7 +50,7 @@ public async Task Should_Not_Allow_To_Delete_If_The_Entity_Has_Changed() //And deleting my old entity throws exception! await Assert.ThrowsAsync(async () => { - await CityRepository.DeleteAsync(london1); + await CityRepository.HardDeleteAsync(london1); }); } }