Browse Source

`SetModificationAuditProperties` for entities with default value.

pull/16520/head
maliming 3 years ago
parent
commit
e90d1659bb
No known key found for this signature in database GPG Key ID: A646B9CB645ECEA4
  1. 2
      framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs
  2. 22
      framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Auditing/Auditing_Tests.cs
  3. 3
      framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/TestMigrationsDbContext.cs
  4. 3
      framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/TestApp/EntityFrameworkCore/TestAppDbContext.cs
  5. 2
      framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Domain/Person.cs

2
framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs

@ -462,7 +462,7 @@ public abstract class AbpDbContext<TDbContext> : DbContext, IAbpEfCoreDbContext,
protected virtual void ApplyAbpConceptsForModifiedEntity(EntityEntry entry) 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); IncrementEntityVersionProperty(entry);
SetModificationAuditProperties(entry); SetModificationAuditProperties(entry);

22
framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Auditing/Auditing_Tests.cs

@ -29,6 +29,28 @@ public class Auditing_Tests : Auditing_Tests<AbpEntityFrameworkCoreTestModule>
})); }));
} }
[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] [Fact]
public async Task Should_Set_Modification_If_Properties_Not_Generated_By_Database() public async Task Should_Set_Modification_If_Properties_Not_Generated_By_Database()
{ {

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

@ -20,7 +20,7 @@ public class TestMigrationsDbContext : AbpDbContext<TestMigrationsDbContext>
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 DbSet<Product> Products { get; set; } public DbSet<Product> Products { get; set; }
public TestMigrationsDbContext(DbContextOptions<TestMigrationsDbContext> options) public TestMigrationsDbContext(DbContextOptions<TestMigrationsDbContext> options)
@ -44,6 +44,7 @@ public class TestMigrationsDbContext : AbpDbContext<TestMigrationsDbContext>
modelBuilder.Entity<Person>(b => modelBuilder.Entity<Person>(b =>
{ {
b.Property(x => x.LastActiveTime).ValueGeneratedOnAddOrUpdate().HasDefaultValue(DateTime.Now); b.Property(x => x.LastActiveTime).ValueGeneratedOnAddOrUpdate().HasDefaultValue(DateTime.Now);
b.Property(x => x.HasDefaultValue).HasDefaultValue(DateTime.Now);
}); });
modelBuilder.Entity<City>(b => modelBuilder.Entity<City>(b =>

3
framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/TestApp/EntityFrameworkCore/TestAppDbContext.cs

@ -29,7 +29,7 @@ public class TestAppDbContext : AbpDbContext<TestAppDbContext>, IThirdDbContext,
public DbSet<Author> Author { get; set; } public DbSet<Author> Author { get; set; }
public DbSet<FourthDbContextDummyEntity> FourthDummyEntities { get; set; } public DbSet<FourthDbContextDummyEntity> FourthDummyEntities { get; set; }
public DbSet<Product> Products { get; set; } public DbSet<Product> Products { get; set; }
public TestAppDbContext(DbContextOptions<TestAppDbContext> options) public TestAppDbContext(DbContextOptions<TestAppDbContext> options)
@ -60,6 +60,7 @@ public class TestAppDbContext : AbpDbContext<TestAppDbContext>, IThirdDbContext,
modelBuilder.Entity<Person>(b => modelBuilder.Entity<Person>(b =>
{ {
b.Property(x => x.LastActiveTime).ValueGeneratedOnAddOrUpdate().HasDefaultValue(DateTime.Now); b.Property(x => x.LastActiveTime).ValueGeneratedOnAddOrUpdate().HasDefaultValue(DateTime.Now);
b.Property(x => x.HasDefaultValue).HasDefaultValue(DateTime.Now);
}); });
modelBuilder modelBuilder

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

@ -30,6 +30,8 @@ public class Person : FullAuditedAggregateRoot<Guid>, IMultiTenant, IHasEntityVe
public virtual DateTime LastActiveTime { get; set; } public virtual DateTime LastActiveTime { get; set; }
public virtual DateTime HasDefaultValue { get; set; }
public int EntityVersion { get; set; } public int EntityVersion { get; set; }
private Person() private Person()

Loading…
Cancel
Save