Browse Source

Don't set modified audit if the property is generated by the database.

Resolve #9166
pull/9192/head
maliming 5 years ago
parent
commit
a44c8a11d6
  1. 23
      framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs
  2. 46
      framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Auditing/Auditing_Tests.cs
  3. 5
      framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/TestApp/EntityFrameworkCore/TestAppDbContext.cs
  4. 5
      framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Domain/Person.cs

23
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) protected virtual void ApplyAbpConceptsForModifiedEntity(EntityEntry entry, EntityChangeReport changeReport)
{ {
UpdateConcurrencyStamp(entry); if (entry.Properties.Any(x => x.IsModified && x.Metadata.ValueGenerated == ValueGenerated.Never))
SetModificationAuditProperties(entry);
if (entry.Entity is ISoftDelete && entry.Entity.As<ISoftDelete>().IsDeleted)
{
SetDeletionAuditProperties(entry);
changeReport.ChangedEntities.Add(new EntityChangeEntry(entry.Entity, EntityChangeType.Deleted));
}
else
{ {
changeReport.ChangedEntities.Add(new EntityChangeEntry(entry.Entity, EntityChangeType.Updated)); UpdateConcurrencyStamp(entry);
SetModificationAuditProperties(entry);
if (entry.Entity is ISoftDelete && entry.Entity.As<ISoftDelete>().IsDeleted)
{
SetDeletionAuditProperties(entry);
changeReport.ChangedEntities.Add(new EntityChangeEntry(entry.Entity, EntityChangeType.Deleted));
}
else
{
changeReport.ChangedEntities.Add(new EntityChangeEntry(entry.Entity, EntityChangeType.Updated));
}
} }
} }

46
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 namespace Volo.Abp.EntityFrameworkCore.Auditing
{ {
public class Auditing_Tests : Auditing_Tests<AbpEntityFrameworkCoreTestModule> public class Auditing_Tests : Auditing_Tests<AbpEntityFrameworkCoreTestModule>
{ {
[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);
}));
}
} }
} }

5
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}); b.HasKey(p => new {p.PersonId, p.Number});
}); });
modelBuilder.Entity<Person>(b =>
{
b.Property(x => x.LastActiveTime).ValueGeneratedOnAddOrUpdate();
});
modelBuilder modelBuilder
.Entity<PersonView>(p => .Entity<PersonView>(p =>
{ {

5
framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Domain/Person.cs

@ -23,9 +23,10 @@ namespace Volo.Abp.TestApp.Domain
public virtual Collection<Phone> Phones { get; set; } public virtual Collection<Phone> Phones { get; set; }
public virtual DateTime? LastActiveTime { get; set; }
private Person() private Person()
{ {
} }
public Person(Guid id, string name, int age, Guid? tenantId = null, Guid? cityId = null) public Person(Guid id, string name, int age, Guid? tenantId = null, Guid? cityId = null)
@ -65,4 +66,4 @@ namespace Volo.Abp.TestApp.Domain
); );
} }
} }
} }

Loading…
Cancel
Save