Browse Source

Merge pull request #9192 from abpframework/maliming/ValueGenerated

Don't set modified audit if the property is generated by the database.
pull/9264/head
Halil İbrahim Kalkan 5 years ago
committed by GitHub
parent
commit
56b017ed26
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  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. 13
      framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/TestMigrationsDbContext.cs
  4. 12
      framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/TestApp/EntityFrameworkCore/TestAppDbContext.cs
  5. 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.State == EntityState.Modified && 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);
}));
}
} }
} }

13
framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/TestMigrationsDbContext.cs

@ -1,3 +1,4 @@
using System;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore.TestApp.SecondContext; using Volo.Abp.EntityFrameworkCore.TestApp.SecondContext;
using Volo.Abp.EntityFrameworkCore.TestApp.ThirdDbContext; using Volo.Abp.EntityFrameworkCore.TestApp.ThirdDbContext;
@ -16,10 +17,10 @@ namespace Volo.Abp.EntityFrameworkCore
public DbSet<BookInSecondDbContext> Books { get; set; } public DbSet<BookInSecondDbContext> Books { get; set; }
public DbSet<EntityWithIntPk> EntityWithIntPks { get; set; } public DbSet<EntityWithIntPk> EntityWithIntPks { get; set; }
public DbSet<Author> Author { get; set; } public DbSet<Author> Author { get; set; }
public TestMigrationsDbContext(DbContextOptions<TestMigrationsDbContext> options) public TestMigrationsDbContext(DbContextOptions<TestMigrationsDbContext> options)
: base(options) : base(options)
{ {
@ -36,6 +37,12 @@ namespace Volo.Abp.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().HasDefaultValue(DateTime.Now);
});
modelBuilder.Entity<City>(b => modelBuilder.Entity<City>(b =>
{ {
b.OwnsMany(c => c.Districts, d => b.OwnsMany(c => c.Districts, d =>

12
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;
using Volo.Abp.EntityFrameworkCore.TestApp.ThirdDbContext; using Volo.Abp.EntityFrameworkCore.TestApp.ThirdDbContext;
using Volo.Abp.TestApp.Domain; using Volo.Abp.TestApp.Domain;
@ -16,10 +17,10 @@ namespace Volo.Abp.TestApp.EntityFrameworkCore
public DbSet<ThirdDbContextDummyEntity> DummyEntities { get; set; } public DbSet<ThirdDbContextDummyEntity> DummyEntities { get; set; }
public DbSet<EntityWithIntPk> EntityWithIntPks { get; set; } public DbSet<EntityWithIntPk> EntityWithIntPks { get; set; }
public DbSet<Author> Author { get; set; } public DbSet<Author> Author { get; set; }
public TestAppDbContext(DbContextOptions<TestAppDbContext> options) public TestAppDbContext(DbContextOptions<TestAppDbContext> options)
: base(options) : base(options)
{ {
@ -36,6 +37,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().HasDefaultValue(DateTime.Now);
});
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