From 96a7303fc6a73eea9eb81f1e01040c68ad5c628d Mon Sep 17 00:00:00 2001 From: maliming Date: Wed, 31 Dec 2025 18:17:36 +0800 Subject: [PATCH] Revert "Add support and tests for entity history with JSON properties" This reverts commit 4d00ee9365dd8fd09716caaf86c6371c660ce61b. --- .../EntityHistory/EntityHistoryHelper.cs | 10 +- .../AbpEntityFrameworkCoreTestModule.cs | 7 - .../Auditing/EntityHistoryHelper_Tests.cs | 130 ------------------ .../TestMigrationsDbContext.cs | 23 ---- .../EntityFrameworkCore/TestAppDbContext.cs | 25 +--- .../Domain/AppEntityWithJsonProperty.cs | 28 ---- 6 files changed, 6 insertions(+), 217 deletions(-) delete mode 100644 framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Auditing/EntityHistoryHelper_Tests.cs delete mode 100644 framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Domain/AppEntityWithJsonProperty.cs 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 e659689da1..2fcdb35480 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 @@ -202,6 +202,11 @@ public class EntityHistoryHelper : IEntityHistoryHelper, ITransientDependency } } + if (AbpEfCoreNavigationHelper == null) + { + return propertyChanges; + } + foreach (var (navigationEntry, index) in entityEntry.Navigations.Select((value, i) => ( value, i ))) { var propertyInfo = navigationEntry.Metadata.PropertyInfo; @@ -225,11 +230,6 @@ public class EntityHistoryHelper : IEntityHistoryHelper, ITransientDependency continue; } - - if (AbpEfCoreNavigationHelper == null) - { - return propertyChanges; - } if (AbpEfCoreNavigationHelper.IsNavigationEntryModified(entityEntry, index)) { 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 e4183a5a19..a7b5c1e602 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 @@ -5,7 +5,6 @@ using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Storage; using Microsoft.Extensions.DependencyInjection; -using Volo.Abp.Auditing; using Volo.Abp.Autofac; using Volo.Abp.Domain.Repositories; using Volo.Abp.EntityFrameworkCore.Domain; @@ -87,12 +86,6 @@ public class AbpEntityFrameworkCoreTestModule : AbpModule abpDbContextConfigurationContext.DbContextOptions.UseSqlite(sqliteConnection).AddAbpDbContextOptionsExtension(); }); }); - - Configure(options => - { - options.EntityHistorySelectors.Add(new NamedTypeSelector(nameof(AppEntityWithJsonProperty), type => type == typeof(AppEntityWithJsonProperty))); - options.EntityHistorySelectors.Add(new NamedTypeSelector(nameof(TestSharedEntity), type => type == typeof(TestSharedEntity))); - }); } public override void OnPreApplicationInitialization(ApplicationInitializationContext context) diff --git a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Auditing/EntityHistoryHelper_Tests.cs b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Auditing/EntityHistoryHelper_Tests.cs deleted file mode 100644 index 65873dd514..0000000000 --- a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Auditing/EntityHistoryHelper_Tests.cs +++ /dev/null @@ -1,130 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using Microsoft.Extensions.DependencyInjection; -using Shouldly; -using Volo.Abp.Auditing; -using Volo.Abp.Data; -using Volo.Abp.Domain.Repositories; -using Volo.Abp.EntityFrameworkCore.EntityHistory; -using Volo.Abp.TestApp.Domain; -using Volo.Abp.TestApp.EntityFrameworkCore; -using Volo.Abp.Uow; -using Xunit; - -namespace Volo.Abp.EntityFrameworkCore.Auditing; - -public class EntityHistoryHelper_Tests : EntityFrameworkCoreTestBase -{ - private readonly IEntityHistoryHelper _entityHistoryHelper; - private readonly IRepository _appEntityWithJsonRepository; - private readonly IRepository _testSharedEntityRepository; - private readonly IUnitOfWorkManager _unitOfWorkManager; - - public EntityHistoryHelper_Tests() - { - _entityHistoryHelper = GetRequiredService(); - _appEntityWithJsonRepository = GetRequiredService>(); - _testSharedEntityRepository = GetRequiredService>(); - _unitOfWorkManager = GetRequiredService(); - } - - [Fact] - public async Task CreateChangeList_Should_Track_Nested_Json_Property_Changes_As_Separate_Property_Changes() - { - // Arrange & Act - EntityChangeInfo entityChange = null; - - await WithUnitOfWorkAsync(async () => - { - var entity = new AppEntityWithJsonProperty(Guid.NewGuid(), "Test Entity") - { - Data = new JsonPropertyObject() - { - { "Name", "String Name" }, - { "Value", "String Value"} - }, - Count = 10 - }; - - await _appEntityWithJsonRepository.InsertAsync(entity); - - var dbContext = await GetDbContextAsync(); - - var entries = dbContext.ChangeTracker.Entries().ToList(); - var entityChanges = _entityHistoryHelper.CreateChangeList(entries); - - entityChange = entityChanges.FirstOrDefault(x => x.EntityTypeFullName.Contains(nameof(AppEntityWithJsonProperty))); - }); - - // Assert - entityChange.ShouldNotBeNull(); - var dataPropertyChange = entityChange.PropertyChanges.FirstOrDefault(x => x.PropertyName == nameof(AppEntityWithJsonProperty.Data)); - dataPropertyChange.ShouldBeNull(); - var jsonNamePropertyChange = entityChange.PropertyChanges.FirstOrDefault(x => x.PropertyName == nameof(AppEntityWithJsonProperty.Data) + "." + "Name"); - jsonNamePropertyChange.ShouldNotBeNull(); - jsonNamePropertyChange.PropertyTypeFullName.ShouldBe(typeof(string).FullName); - jsonNamePropertyChange.NewValue.ShouldBe("\"String Name\""); - - var jsonValuePropertyChange = entityChange.PropertyChanges.FirstOrDefault(x => x.PropertyName == "Value"); - jsonValuePropertyChange.ShouldNotBeNull(); - jsonValuePropertyChange.PropertyTypeFullName.ShouldBe(typeof(string).FullName); - jsonValuePropertyChange.NewValue.ShouldBe("\"String Value\""); - } - - [Fact] - public async Task CreateChangeList_Should_Track_Shared_Entities_With_Their_Respective_Entity_Names() - { - // Arrange & Act - List entityChanges = null; - - await WithUnitOfWorkAsync(async () => - { - var entity = new TestSharedEntity(Guid.NewGuid()) - { - TenantId = null, - IsDeleted = false, - Name = "Test Person1", - Age = 10, - Birthday = DateTime.Now - }.SetProperty("testProperty", "Test Value1"); - - _testSharedEntityRepository.SetEntityName("TestSharedEntity1"); - await _testSharedEntityRepository.InsertAsync(entity); - - var entity2 = new TestSharedEntity(Guid.NewGuid()) - { - TenantId = null, - IsDeleted = false, - Name = "Test Person2", - Age = 20, - Birthday = DateTime.Now - }.SetProperty("testProperty", "Test Value2"); - - _testSharedEntityRepository.SetEntityName("TestSharedEntity2"); - await _testSharedEntityRepository.InsertAsync(entity2); - - var dbContext = await GetDbContextAsync(); - - var entries = dbContext.ChangeTracker.Entries().ToList(); - entityChanges = _entityHistoryHelper.CreateChangeList(entries); - }); - - entityChanges.ShouldContain(x => x.EntityTypeFullName == "TestSharedEntity1"); - entityChanges.ShouldContain(x => x.EntityTypeFullName == "TestSharedEntity2"); - } - - private async Task GetDbContextAsync() - { - var uow = _unitOfWorkManager.Current; - if (uow == null) - { - throw new InvalidOperationException("No active unit of work found"); - } - - var dbContextProvider = uow.ServiceProvider.GetRequiredService>(); - return await dbContextProvider.GetDbContextAsync(); - } -} - 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 6e102706fc..4c6efa5a02 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 @@ -37,8 +37,6 @@ public class TestMigrationsDbContext : AbpDbContext public DbSet TestSharedEntity => Set("TestSharedEntity1"); public DbSet TestSharedEntity2 => Set("TestSharedEntity2"); - - public DbSet EntitiesWithObjectProperty { get; set; } public TestMigrationsDbContext(DbContextOptions options) : base(options) @@ -142,26 +140,5 @@ public class TestMigrationsDbContext : AbpDbContext { b.ConfigureByConvention(); }); - - modelBuilder.Entity(b => - { - b.ConfigureByConvention(); - b.OwnsOne(x => x.Data, b2 => - { - b2.ToJson(); - - b2.Property("Name") - .HasConversion( - v => v.ToString(), - v => v - ); - - b2.Property("Value") - .HasConversion( - v => v.ToString(), - v => v - ); - }); - }); } } 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 b0b0a55c03..a8e68dfca8 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 @@ -40,11 +40,9 @@ public class TestAppDbContext : AbpDbContext, IThirdDbContext, public DbSet Blogs { get; set; } public DbSet BlogPosts { get; set; } - + public DbSet TestSharedEntity => Set("TestSharedEntity1"); public DbSet TestSharedEntity2 => Set("TestSharedEntity2"); - - public DbSet EntitiesWithObjectProperty { get; set; } public TestAppDbContext(DbContextOptions options) : base(options) @@ -168,27 +166,6 @@ public class TestAppDbContext : AbpDbContext, IThirdDbContext, b.ConfigureByConvention(); }); - modelBuilder.Entity(b => - { - b.ConfigureByConvention(); - b.OwnsOne(x => x.Data, b2 => - { - b2.ToJson(); - - b2.Property("Name") - .HasConversion( - v => v.ToString(), - v => v - ); - - b2.Property("Value") - .HasConversion( - v => v.ToString(), - v => v - ); - }); - }); - modelBuilder.TryConfigureObjectExtensions(); } } diff --git a/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Domain/AppEntityWithJsonProperty.cs b/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Domain/AppEntityWithJsonProperty.cs deleted file mode 100644 index ab830e3e85..0000000000 --- a/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Domain/AppEntityWithJsonProperty.cs +++ /dev/null @@ -1,28 +0,0 @@ -using System; -using System.Collections.Generic; -using Volo.Abp.Domain.Entities.Auditing; - -namespace Volo.Abp.TestApp.Domain; - -public class AppEntityWithJsonProperty : FullAuditedAggregateRoot -{ - public string Name { get; set; } - - public JsonPropertyObject Data { get; set; } - - public int Count { get; set; } - - public AppEntityWithJsonProperty() - { - } - - public AppEntityWithJsonProperty(Guid id, string name) : base(id) - { - Name = name; - } -} - -public class JsonPropertyObject : Dictionary -{ -} -