From e90d1659bb512d64f3e2acc62883be94b98b4cdf Mon Sep 17 00:00:00 2001 From: maliming Date: Wed, 10 May 2023 08:58:19 +0800 Subject: [PATCH] `SetModificationAuditProperties` for entities with default value. --- .../Abp/EntityFrameworkCore/AbpDbContext.cs | 2 +- .../Auditing/Auditing_Tests.cs | 22 +++++++++++++++++++ .../TestMigrationsDbContext.cs | 3 ++- .../EntityFrameworkCore/TestAppDbContext.cs | 3 ++- .../Volo/Abp/TestApp/Domain/Person.cs | 2 ++ 5 files changed, 29 insertions(+), 3 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 33fdadc9b4..0a5eaa851e 100644 --- a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs +++ b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs @@ -462,7 +462,7 @@ public abstract class AbpDbContext : DbContext, IAbpEfCoreDbContext, protected virtual void ApplyAbpConceptsForModifiedEntity(EntityEntry entry) { - if (entry.State == EntityState.Modified && entry.Properties.Any(x => x.IsModified && x.Metadata.ValueGenerated == ValueGenerated.Never)) + if (entry.State == EntityState.Modified && entry.Properties.Any(x => x.IsModified && (x.Metadata.ValueGenerated == ValueGenerated.Never || x.Metadata.ValueGenerated == ValueGenerated.OnAdd))) { IncrementEntityVersionProperty(entry); SetModificationAuditProperties(entry); diff --git a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Auditing/Auditing_Tests.cs b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Auditing/Auditing_Tests.cs index 3f6e11c02d..8ad0029cc0 100644 --- a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Auditing/Auditing_Tests.cs +++ b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Auditing/Auditing_Tests.cs @@ -29,6 +29,28 @@ public class Auditing_Tests : Auditing_Tests })); } + [Fact] + public async Task Should_Set_Modification_If_Properties_Changed_With_Default_Value() + { + var date = DateTime.Parse("2022-01-01"); + await WithUnitOfWorkAsync((async () => + { + var douglas = await PersonRepository.GetAsync(TestDataBuilder.UserDouglasId); + douglas.HasDefaultValue = date; + })); + + await WithUnitOfWorkAsync((async () => + { + var douglas = await PersonRepository.FindAsync(TestDataBuilder.UserDouglasId); + + douglas.ShouldNotBeNull(); + douglas.HasDefaultValue.ShouldBe(date); + douglas.LastModificationTime.ShouldNotBeNull(); + douglas.LastModificationTime.Value.ShouldBeLessThanOrEqualTo(Clock.Now); + douglas.LastModifierId.ShouldBe(CurrentUserId); + })); + } + [Fact] public async Task Should_Set_Modification_If_Properties_Not_Generated_By_Database() { 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 529912aa68..99e689bdd4 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 @@ -20,7 +20,7 @@ public class TestMigrationsDbContext : AbpDbContext public DbSet EntityWithIntPks { get; set; } public DbSet Author { get; set; } - + public DbSet Products { get; set; } public TestMigrationsDbContext(DbContextOptions options) @@ -44,6 +44,7 @@ public class TestMigrationsDbContext : AbpDbContext modelBuilder.Entity(b => { b.Property(x => x.LastActiveTime).ValueGeneratedOnAddOrUpdate().HasDefaultValue(DateTime.Now); + b.Property(x => x.HasDefaultValue).HasDefaultValue(DateTime.Now); }); 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 921b97f6a4..66772343af 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 @@ -29,7 +29,7 @@ public class TestAppDbContext : AbpDbContext, IThirdDbContext, public DbSet Author { get; set; } public DbSet FourthDummyEntities { get; set; } - + public DbSet Products { get; set; } public TestAppDbContext(DbContextOptions options) @@ -60,6 +60,7 @@ public class TestAppDbContext : AbpDbContext, IThirdDbContext, modelBuilder.Entity(b => { b.Property(x => x.LastActiveTime).ValueGeneratedOnAddOrUpdate().HasDefaultValue(DateTime.Now); + b.Property(x => x.HasDefaultValue).HasDefaultValue(DateTime.Now); }); modelBuilder diff --git a/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Domain/Person.cs b/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Domain/Person.cs index 0726126eba..5d78236fca 100644 --- a/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Domain/Person.cs +++ b/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Domain/Person.cs @@ -30,6 +30,8 @@ public class Person : FullAuditedAggregateRoot, IMultiTenant, IHasEntityVe public virtual DateTime LastActiveTime { get; set; } + public virtual DateTime HasDefaultValue { get; set; } + public int EntityVersion { get; set; } private Person()