Browse Source

Merge pull request #20405 from abpframework/auto-merge/rel-8-3/2868

Merge branch dev with rel-8.3
pull/20423/head
maliming 2 years ago
committed by GitHub
parent
commit
9f1ccba8bb
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 2
      framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs
  2. 7
      framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/ChangeTrackers/AbpEfCoreNavigationHelper.cs
  3. 24
      framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DomainEvents/DomainEvents_Tests.cs

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

@ -282,7 +282,7 @@ public abstract class AbpDbContext<TDbContext> : DbContext, IAbpEfCoreDbContext,
finally finally
{ {
ChangeTracker.AutoDetectChangesEnabled = true; ChangeTracker.AutoDetectChangesEnabled = true;
AbpEfCoreNavigationHelper.Clear(); AbpEfCoreNavigationHelper.RemoveChangedEntityEntries();
} }
} }

7
framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/ChangeTrackers/AbpEfCoreNavigationHelper.cs

@ -199,7 +199,12 @@ public class AbpEfCoreNavigationHelper : ITransientDependency
return null; return null;
} }
public void Clear() public virtual void RemoveChangedEntityEntries()
{
EntityEntries.RemoveAll(x => x.Value.IsModified);
}
public virtual void Clear()
{ {
EntityEntries.Clear(); EntityEntries.Clear();
} }

24
framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DomainEvents/DomainEvents_Tests.cs

@ -27,7 +27,7 @@ public class AbpEfCoreDomainEvents_Tests : EntityFrameworkCoreTestBase
protected readonly IRepository<AppEntityWithNavigations, Guid> AppEntityWithNavigationsRepository; protected readonly IRepository<AppEntityWithNavigations, Guid> AppEntityWithNavigationsRepository;
protected readonly ILocalEventBus LocalEventBus; protected readonly ILocalEventBus LocalEventBus;
protected readonly IRepository<Person, Guid> PersonRepository; protected readonly IRepository<Person, Guid> PersonRepository;
protected bool _loadEntityWithDetails = false; protected bool _loadEntityWithoutDetails = false;
public AbpEfCoreDomainEvents_Tests() public AbpEfCoreDomainEvents_Tests()
{ {
@ -42,7 +42,7 @@ public class AbpEfCoreDomainEvents_Tests : EntityFrameworkCoreTestBase
{ {
options.Entity<AppEntityWithNavigations>(opt => options.Entity<AppEntityWithNavigations>(opt =>
{ {
if (_loadEntityWithDetails) if (_loadEntityWithoutDetails)
{ {
opt.DefaultWithDetailsFunc = q => q; opt.DefaultWithDetailsFunc = q => q;
} }
@ -55,7 +55,7 @@ public class AbpEfCoreDomainEvents_Tests : EntityFrameworkCoreTestBase
[Fact] [Fact]
public async Task Should_Trigger_Domain_Events_For_Aggregate_Root_When_Navigation_Changes_Tests() public async Task Should_Trigger_Domain_Events_For_Aggregate_Root_When_Navigation_Changes_Tests()
{ {
_loadEntityWithDetails = false; _loadEntityWithoutDetails = false;
var entityId = Guid.NewGuid(); var entityId = Guid.NewGuid();
@ -77,6 +77,8 @@ public class AbpEfCoreDomainEvents_Tests : EntityFrameworkCoreTestBase
await PersonRepository.InsertAsync(new Person(Guid.NewGuid(), Guid.NewGuid().ToString(), new Random().Next(1, 100))); await PersonRepository.InsertAsync(new Person(Guid.NewGuid(), Guid.NewGuid().ToString(), new Random().Next(1, 100)));
}); });
var unitOfWorkManager = ServiceProvider.GetRequiredService<IUnitOfWorkManager>();
// Test with simple property // Test with simple property
await WithUnitOfWorkAsync(async () => await WithUnitOfWorkAsync(async () =>
{ {
@ -92,6 +94,7 @@ public class AbpEfCoreDomainEvents_Tests : EntityFrameworkCoreTestBase
await WithUnitOfWorkAsync(async () => await WithUnitOfWorkAsync(async () =>
{ {
var entity = await AppEntityWithNavigationsRepository.GetAsync(entityId); var entity = await AppEntityWithNavigationsRepository.GetAsync(entityId);
await unitOfWorkManager.Current.SaveChangesAsync();
entity.AppEntityWithValueObjectAddress = new AppEntityWithValueObjectAddress("Turkey"); entity.AppEntityWithValueObjectAddress = new AppEntityWithValueObjectAddress("Turkey");
await AppEntityWithNavigationsRepository.UpdateAsync(entity); await AppEntityWithNavigationsRepository.UpdateAsync(entity);
}); });
@ -102,6 +105,7 @@ public class AbpEfCoreDomainEvents_Tests : EntityFrameworkCoreTestBase
await WithUnitOfWorkAsync(async () => await WithUnitOfWorkAsync(async () =>
{ {
var entity = await AppEntityWithNavigationsRepository.GetAsync(entityId); var entity = await AppEntityWithNavigationsRepository.GetAsync(entityId);
await unitOfWorkManager.Current.SaveChangesAsync();
entity.AppEntityWithValueObjectAddress.Country = "USA"; entity.AppEntityWithValueObjectAddress.Country = "USA";
await AppEntityWithNavigationsRepository.UpdateAsync(entity); await AppEntityWithNavigationsRepository.UpdateAsync(entity);
}); });
@ -117,6 +121,7 @@ public class AbpEfCoreDomainEvents_Tests : EntityFrameworkCoreTestBase
await WithUnitOfWorkAsync(async () => await WithUnitOfWorkAsync(async () =>
{ {
var entity = await AppEntityWithNavigationsRepository.GetAsync(entityId); var entity = await AppEntityWithNavigationsRepository.GetAsync(entityId);
await unitOfWorkManager.Current.SaveChangesAsync();
entity.AppEntityWithValueObjectAddress = null; entity.AppEntityWithValueObjectAddress = null;
await AppEntityWithNavigationsRepository.UpdateAsync(entity); await AppEntityWithNavigationsRepository.UpdateAsync(entity);
}); });
@ -128,6 +133,7 @@ public class AbpEfCoreDomainEvents_Tests : EntityFrameworkCoreTestBase
await WithUnitOfWorkAsync(async () => await WithUnitOfWorkAsync(async () =>
{ {
var entity = await AppEntityWithNavigationsRepository.GetAsync(entityId); var entity = await AppEntityWithNavigationsRepository.GetAsync(entityId);
await unitOfWorkManager.Current.SaveChangesAsync();
entity.OneToOne = new AppEntityWithNavigationChildOneToOne entity.OneToOne = new AppEntityWithNavigationChildOneToOne
{ {
ChildName = "ChildName", ChildName = "ChildName",
@ -157,6 +163,7 @@ public class AbpEfCoreDomainEvents_Tests : EntityFrameworkCoreTestBase
await WithUnitOfWorkAsync(async () => await WithUnitOfWorkAsync(async () =>
{ {
var entity = await AppEntityWithNavigationsRepository.GetAsync(entityId); var entity = await AppEntityWithNavigationsRepository.GetAsync(entityId);
await unitOfWorkManager.Current.SaveChangesAsync();
entity.OneToOne.ChildName = "ChildName2"; entity.OneToOne.ChildName = "ChildName2";
await AppEntityWithNavigationsRepository.UpdateAsync(entity); await AppEntityWithNavigationsRepository.UpdateAsync(entity);
}); });
@ -171,6 +178,7 @@ public class AbpEfCoreDomainEvents_Tests : EntityFrameworkCoreTestBase
await WithUnitOfWorkAsync(async () => await WithUnitOfWorkAsync(async () =>
{ {
var entity = await AppEntityWithNavigationsRepository.GetAsync(entityId); var entity = await AppEntityWithNavigationsRepository.GetAsync(entityId);
await unitOfWorkManager.Current.SaveChangesAsync();
entity.OneToOne.OneToOne.ChildName = "OneToOne-ChildName2"; entity.OneToOne.OneToOne.ChildName = "OneToOne-ChildName2";
await AppEntityWithNavigationsRepository.UpdateAsync(entity); await AppEntityWithNavigationsRepository.UpdateAsync(entity);
}); });
@ -188,6 +196,7 @@ public class AbpEfCoreDomainEvents_Tests : EntityFrameworkCoreTestBase
await WithUnitOfWorkAsync(async () => await WithUnitOfWorkAsync(async () =>
{ {
var entity = await AppEntityWithNavigationsRepository.GetAsync(entityId); var entity = await AppEntityWithNavigationsRepository.GetAsync(entityId);
await unitOfWorkManager.Current.SaveChangesAsync();
entity.OneToOne = null; entity.OneToOne = null;
await AppEntityWithNavigationsRepository.UpdateAsync(entity); await AppEntityWithNavigationsRepository.UpdateAsync(entity);
}); });
@ -199,6 +208,7 @@ public class AbpEfCoreDomainEvents_Tests : EntityFrameworkCoreTestBase
await WithUnitOfWorkAsync(async () => await WithUnitOfWorkAsync(async () =>
{ {
var entity = await AppEntityWithNavigationsRepository.GetAsync(entityId); var entity = await AppEntityWithNavigationsRepository.GetAsync(entityId);
await unitOfWorkManager.Current.SaveChangesAsync();
entity.OneToMany = new List<AppEntityWithNavigationChildOneToMany>() entity.OneToMany = new List<AppEntityWithNavigationChildOneToMany>()
{ {
new AppEntityWithNavigationChildOneToMany new AppEntityWithNavigationChildOneToMany
@ -235,6 +245,7 @@ public class AbpEfCoreDomainEvents_Tests : EntityFrameworkCoreTestBase
await WithUnitOfWorkAsync(async () => await WithUnitOfWorkAsync(async () =>
{ {
var entity = await AppEntityWithNavigationsRepository.GetAsync(entityId); var entity = await AppEntityWithNavigationsRepository.GetAsync(entityId);
await unitOfWorkManager.Current.SaveChangesAsync();
entity.OneToMany[0].ChildName = "ChildName2"; entity.OneToMany[0].ChildName = "ChildName2";
await AppEntityWithNavigationsRepository.UpdateAsync(entity); await AppEntityWithNavigationsRepository.UpdateAsync(entity);
}); });
@ -249,6 +260,7 @@ public class AbpEfCoreDomainEvents_Tests : EntityFrameworkCoreTestBase
await WithUnitOfWorkAsync(async () => await WithUnitOfWorkAsync(async () =>
{ {
var entity = await AppEntityWithNavigationsRepository.GetAsync(entityId); var entity = await AppEntityWithNavigationsRepository.GetAsync(entityId);
await unitOfWorkManager.Current.SaveChangesAsync();
entity.OneToMany[0].OneToMany[0].ChildName = "OneToMany-ChildName2"; entity.OneToMany[0].OneToMany[0].ChildName = "OneToMany-ChildName2";
await AppEntityWithNavigationsRepository.UpdateAsync(entity); await AppEntityWithNavigationsRepository.UpdateAsync(entity);
}); });
@ -266,6 +278,7 @@ public class AbpEfCoreDomainEvents_Tests : EntityFrameworkCoreTestBase
await WithUnitOfWorkAsync(async () => await WithUnitOfWorkAsync(async () =>
{ {
var entity = await AppEntityWithNavigationsRepository.GetAsync(entityId); var entity = await AppEntityWithNavigationsRepository.GetAsync(entityId);
await unitOfWorkManager.Current.SaveChangesAsync();
entity.OneToMany.Clear(); entity.OneToMany.Clear();
await AppEntityWithNavigationsRepository.UpdateAsync(entity); await AppEntityWithNavigationsRepository.UpdateAsync(entity);
}); });
@ -277,6 +290,7 @@ public class AbpEfCoreDomainEvents_Tests : EntityFrameworkCoreTestBase
await WithUnitOfWorkAsync(async () => await WithUnitOfWorkAsync(async () =>
{ {
var entity = await AppEntityWithNavigationsRepository.GetAsync(entityId); var entity = await AppEntityWithNavigationsRepository.GetAsync(entityId);
await unitOfWorkManager.Current.SaveChangesAsync();
entity.ManyToMany = new List<AppEntityWithNavigationChildManyToMany>() entity.ManyToMany = new List<AppEntityWithNavigationChildManyToMany>()
{ {
new AppEntityWithNavigationChildManyToMany new AppEntityWithNavigationChildManyToMany
@ -293,6 +307,7 @@ public class AbpEfCoreDomainEvents_Tests : EntityFrameworkCoreTestBase
await WithUnitOfWorkAsync(async () => await WithUnitOfWorkAsync(async () =>
{ {
var entity = await AppEntityWithNavigationsRepository.GetAsync(entityId); var entity = await AppEntityWithNavigationsRepository.GetAsync(entityId);
await unitOfWorkManager.Current.SaveChangesAsync();
entity.ManyToMany[0].ChildName = "ChildName2"; entity.ManyToMany[0].ChildName = "ChildName2";
await AppEntityWithNavigationsRepository.UpdateAsync(entity); await AppEntityWithNavigationsRepository.UpdateAsync(entity);
}); });
@ -303,6 +318,7 @@ public class AbpEfCoreDomainEvents_Tests : EntityFrameworkCoreTestBase
await WithUnitOfWorkAsync(async () => await WithUnitOfWorkAsync(async () =>
{ {
var entity = await AppEntityWithNavigationsRepository.GetAsync(entityId); var entity = await AppEntityWithNavigationsRepository.GetAsync(entityId);
await unitOfWorkManager.Current.SaveChangesAsync();
entity.ManyToMany.Clear(); entity.ManyToMany.Clear();
await AppEntityWithNavigationsRepository.UpdateAsync(entity); await AppEntityWithNavigationsRepository.UpdateAsync(entity);
}); });
@ -313,7 +329,7 @@ public class AbpEfCoreDomainEvents_Tests : EntityFrameworkCoreTestBase
[Fact] [Fact]
public async Task Should_Trigger_Domain_Events_For_Aggregate_Root_When_EnsureCollectionLoaded_Navigation_Changes_Tests() public async Task Should_Trigger_Domain_Events_For_Aggregate_Root_When_EnsureCollectionLoaded_Navigation_Changes_Tests()
{ {
_loadEntityWithDetails = true; _loadEntityWithoutDetails = true;
var entityId = Guid.NewGuid(); var entityId = Guid.NewGuid();

Loading…
Cancel
Save