From a44c8a11d6594582e93564c264a8e4fd25ff7b41 Mon Sep 17 00:00:00 2001 From: maliming Date: Mon, 31 May 2021 09:46:19 +0800 Subject: [PATCH 1/2] Don't set modified audit if the property is generated by the database. Resolve #9166 --- .../Abp/EntityFrameworkCore/AbpDbContext.cs | 23 ++++++---- .../Auditing/Auditing_Tests.cs | 46 ++++++++++++++++++- .../EntityFrameworkCore/TestAppDbContext.cs | 5 ++ .../Volo/Abp/TestApp/Domain/Person.cs | 5 +- 4 files changed, 66 insertions(+), 13 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 cda8c02416..4b3fcd1d0d 100644 --- a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs +++ b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs @@ -372,17 +372,20 @@ namespace Volo.Abp.EntityFrameworkCore protected virtual void ApplyAbpConceptsForModifiedEntity(EntityEntry entry, EntityChangeReport changeReport) { - UpdateConcurrencyStamp(entry); - SetModificationAuditProperties(entry); - - if (entry.Entity is ISoftDelete && entry.Entity.As().IsDeleted) - { - SetDeletionAuditProperties(entry); - changeReport.ChangedEntities.Add(new EntityChangeEntry(entry.Entity, EntityChangeType.Deleted)); - } - else + if (entry.Properties.Any(x => x.IsModified && x.Metadata.ValueGenerated == ValueGenerated.Never)) { - changeReport.ChangedEntities.Add(new EntityChangeEntry(entry.Entity, EntityChangeType.Updated)); + UpdateConcurrencyStamp(entry); + SetModificationAuditProperties(entry); + + if (entry.Entity is ISoftDelete && entry.Entity.As().IsDeleted) + { + SetDeletionAuditProperties(entry); + changeReport.ChangedEntities.Add(new EntityChangeEntry(entry.Entity, EntityChangeType.Deleted)); + } + else + { + changeReport.ChangedEntities.Add(new EntityChangeEntry(entry.Entity, EntityChangeType.Updated)); + } } } 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 d1b1018e5c..ee1ca3572c 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 @@ -1,9 +1,53 @@ -using Volo.Abp.TestApp.Testing; +using System; +using System.Threading.Tasks; +using Shouldly; +using Volo.Abp.TestApp; +using Volo.Abp.TestApp.Testing; +using Xunit; namespace Volo.Abp.EntityFrameworkCore.Auditing { public class Auditing_Tests : Auditing_Tests { + [Fact] + public async Task Should_Not_Set_Modification_If_Properties_Generated_By_Database() + { + await WithUnitOfWorkAsync((async () => + { + var douglas = await PersonRepository.GetAsync(TestDataBuilder.UserDouglasId); + douglas.LastActiveTime = DateTime.Now; + })); + await WithUnitOfWorkAsync((async () => + { + var douglas = await PersonRepository.FindAsync(TestDataBuilder.UserDouglasId); + + douglas.ShouldNotBeNull(); + douglas.LastModificationTime.ShouldBeNull(); + douglas.LastModificationTime.ShouldBeNull(); + douglas.LastModifierId.ShouldBeNull(); + })); + } + + [Fact] + public async Task Should_Set_Modification_If_Properties_Not_Generated_By_Database() + { + await WithUnitOfWorkAsync((async () => + { + var douglas = await PersonRepository.GetAsync(TestDataBuilder.UserDouglasId); + douglas.LastActiveTime = DateTime.Now; + douglas.Age = 100; + })); + + await WithUnitOfWorkAsync((async () => + { + var douglas = await PersonRepository.FindAsync(TestDataBuilder.UserDouglasId); + + douglas.ShouldNotBeNull(); + douglas.LastModificationTime.ShouldNotBeNull(); + douglas.LastModificationTime.Value.ShouldBeLessThanOrEqualTo(Clock.Now); + douglas.LastModifierId.ShouldBe(CurrentUserId); + })); + } } } 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 da8bbc8a6c..9022a1efc4 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 @@ -36,6 +36,11 @@ namespace Volo.Abp.TestApp.EntityFrameworkCore b.HasKey(p => new {p.PersonId, p.Number}); }); + modelBuilder.Entity(b => + { + b.Property(x => x.LastActiveTime).ValueGeneratedOnAddOrUpdate(); + }); + modelBuilder .Entity(p => { 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 9075740f11..0c17d7b250 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 @@ -23,9 +23,10 @@ namespace Volo.Abp.TestApp.Domain public virtual Collection Phones { get; set; } + public virtual DateTime? LastActiveTime { get; set; } + private Person() { - } public Person(Guid id, string name, int age, Guid? tenantId = null, Guid? cityId = null) @@ -65,4 +66,4 @@ namespace Volo.Abp.TestApp.Domain ); } } -} \ No newline at end of file +} From a8995eaeb8ddcabfea202d6ec9daf032a35b870c Mon Sep 17 00:00:00 2001 From: maliming Date: Mon, 31 May 2021 10:10:41 +0800 Subject: [PATCH 2/2] Set the database default value of LastActiveTime. --- .../Volo/Abp/EntityFrameworkCore/AbpDbContext.cs | 2 +- .../EntityFrameworkCore/TestMigrationsDbContext.cs | 13 ++++++++++--- .../TestApp/EntityFrameworkCore/TestAppDbContext.cs | 9 +++++---- .../Volo/Abp/TestApp/Domain/Person.cs | 2 +- 4 files changed, 17 insertions(+), 9 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 4b3fcd1d0d..03a22f5718 100644 --- a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs +++ b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs @@ -372,7 +372,7 @@ namespace Volo.Abp.EntityFrameworkCore protected virtual void ApplyAbpConceptsForModifiedEntity(EntityEntry entry, EntityChangeReport changeReport) { - if (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)) { UpdateConcurrencyStamp(entry); SetModificationAuditProperties(entry); 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 23137f0f0c..516c7d5789 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 @@ -1,3 +1,4 @@ +using System; using Microsoft.EntityFrameworkCore; using Volo.Abp.EntityFrameworkCore.TestApp.SecondContext; using Volo.Abp.EntityFrameworkCore.TestApp.ThirdDbContext; @@ -16,10 +17,10 @@ namespace Volo.Abp.EntityFrameworkCore public DbSet Books { get; set; } public DbSet EntityWithIntPks { get; set; } - + public DbSet Author { get; set; } - - public TestMigrationsDbContext(DbContextOptions options) + + public TestMigrationsDbContext(DbContextOptions options) : base(options) { @@ -36,6 +37,12 @@ namespace Volo.Abp.EntityFrameworkCore b.HasKey(p => new { p.PersonId, p.Number }); }); + + modelBuilder.Entity(b => + { + b.Property(x => x.LastActiveTime).ValueGeneratedOnAddOrUpdate().HasDefaultValue(DateTime.Now); + }); + modelBuilder.Entity(b => { b.OwnsMany(c => c.Districts, d => 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 9022a1efc4..6bc581ca7d 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 @@ -1,4 +1,5 @@ -using Microsoft.EntityFrameworkCore; +using System; +using Microsoft.EntityFrameworkCore; using Volo.Abp.EntityFrameworkCore; using Volo.Abp.EntityFrameworkCore.TestApp.ThirdDbContext; using Volo.Abp.TestApp.Domain; @@ -16,10 +17,10 @@ namespace Volo.Abp.TestApp.EntityFrameworkCore public DbSet DummyEntities { get; set; } public DbSet EntityWithIntPks { get; set; } - + public DbSet Author { get; set; } - public TestAppDbContext(DbContextOptions options) + public TestAppDbContext(DbContextOptions options) : base(options) { @@ -38,7 +39,7 @@ namespace Volo.Abp.TestApp.EntityFrameworkCore modelBuilder.Entity(b => { - b.Property(x => x.LastActiveTime).ValueGeneratedOnAddOrUpdate(); + b.Property(x => x.LastActiveTime).ValueGeneratedOnAddOrUpdate().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 0c17d7b250..f88e617072 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 @@ -23,7 +23,7 @@ namespace Volo.Abp.TestApp.Domain public virtual Collection Phones { get; set; } - public virtual DateTime? LastActiveTime { get; set; } + public virtual DateTime LastActiveTime { get; set; } private Person() {