Skip to content

Commit

Permalink
Merge pull request #20396 from abpframework/AbpEfCoreNavigationHelper
Browse files Browse the repository at this point in the history
Only remove the changed entities at end of `SaveChangesAsync` method.
  • Loading branch information
EngincanV authored Aug 5, 2024
2 parents fc596b0 + 59a6a25 commit 24f41d3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ public override async Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess,
finally
{
ChangeTracker.AutoDetectChangesEnabled = true;
AbpEfCoreNavigationHelper.Clear();
AbpEfCoreNavigationHelper.RemoveChangedEntityEntries();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,12 @@ public virtual bool IsNavigationEntryModified(EntityEntry entityEntry, int? navi
return null;
}

public void Clear()
public virtual void RemoveChangedEntityEntries()
{
EntityEntries.RemoveAll(x => x.Value.IsModified);
}

public virtual void Clear()
{
EntityEntries.Clear();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class AbpEfCoreDomainEvents_Tests : EntityFrameworkCoreTestBase
protected readonly IRepository<AppEntityWithNavigations, Guid> AppEntityWithNavigationsRepository;
protected readonly ILocalEventBus LocalEventBus;
protected readonly IRepository<Person, Guid> PersonRepository;
protected bool _loadEntityWithDetails = false;
protected bool _loadEntityWithoutDetails = false;

public AbpEfCoreDomainEvents_Tests()
{
Expand All @@ -42,7 +42,7 @@ protected override void AfterAddApplication(IServiceCollection services)
{
options.Entity<AppEntityWithNavigations>(opt =>
{
if (_loadEntityWithDetails)
if (_loadEntityWithoutDetails)
{
opt.DefaultWithDetailsFunc = q => q;
}
Expand All @@ -55,7 +55,7 @@ protected override void AfterAddApplication(IServiceCollection services)
[Fact]
public async Task Should_Trigger_Domain_Events_For_Aggregate_Root_When_Navigation_Changes_Tests()
{
_loadEntityWithDetails = false;
_loadEntityWithoutDetails = false;

var entityId = Guid.NewGuid();

Expand All @@ -77,6 +77,8 @@ public async Task Should_Trigger_Domain_Events_For_Aggregate_Root_When_Navigatio
await PersonRepository.InsertAsync(new Person(Guid.NewGuid(), Guid.NewGuid().ToString(), new Random().Next(1, 100)));
});

var unitOfWorkManager = ServiceProvider.GetRequiredService<IUnitOfWorkManager>();

// Test with simple property
await WithUnitOfWorkAsync(async () =>
{
Expand All @@ -92,6 +94,7 @@ await WithUnitOfWorkAsync(async () =>
await WithUnitOfWorkAsync(async () =>
{
var entity = await AppEntityWithNavigationsRepository.GetAsync(entityId);
await unitOfWorkManager.Current.SaveChangesAsync();
entity.AppEntityWithValueObjectAddress = new AppEntityWithValueObjectAddress("Turkey");
await AppEntityWithNavigationsRepository.UpdateAsync(entity);
});
Expand All @@ -102,6 +105,7 @@ await WithUnitOfWorkAsync(async () =>
await WithUnitOfWorkAsync(async () =>
{
var entity = await AppEntityWithNavigationsRepository.GetAsync(entityId);
await unitOfWorkManager.Current.SaveChangesAsync();
entity.AppEntityWithValueObjectAddress.Country = "USA";
await AppEntityWithNavigationsRepository.UpdateAsync(entity);
});
Expand All @@ -117,6 +121,7 @@ await WithUnitOfWorkAsync(async () =>
await WithUnitOfWorkAsync(async () =>
{
var entity = await AppEntityWithNavigationsRepository.GetAsync(entityId);
await unitOfWorkManager.Current.SaveChangesAsync();
entity.AppEntityWithValueObjectAddress = null;
await AppEntityWithNavigationsRepository.UpdateAsync(entity);
});
Expand All @@ -128,6 +133,7 @@ await WithUnitOfWorkAsync(async () =>
await WithUnitOfWorkAsync(async () =>
{
var entity = await AppEntityWithNavigationsRepository.GetAsync(entityId);
await unitOfWorkManager.Current.SaveChangesAsync();
entity.OneToOne = new AppEntityWithNavigationChildOneToOne
{
ChildName = "ChildName",
Expand Down Expand Up @@ -157,6 +163,7 @@ await WithUnitOfWorkAsync(async () =>
await WithUnitOfWorkAsync(async () =>
{
var entity = await AppEntityWithNavigationsRepository.GetAsync(entityId);
await unitOfWorkManager.Current.SaveChangesAsync();
entity.OneToOne.ChildName = "ChildName2";
await AppEntityWithNavigationsRepository.UpdateAsync(entity);
});
Expand All @@ -171,6 +178,7 @@ await WithUnitOfWorkAsync(async () =>
await WithUnitOfWorkAsync(async () =>
{
var entity = await AppEntityWithNavigationsRepository.GetAsync(entityId);
await unitOfWorkManager.Current.SaveChangesAsync();
entity.OneToOne.OneToOne.ChildName = "OneToOne-ChildName2";
await AppEntityWithNavigationsRepository.UpdateAsync(entity);
});
Expand All @@ -188,6 +196,7 @@ await WithUnitOfWorkAsync(async () =>
await WithUnitOfWorkAsync(async () =>
{
var entity = await AppEntityWithNavigationsRepository.GetAsync(entityId);
await unitOfWorkManager.Current.SaveChangesAsync();
entity.OneToOne = null;
await AppEntityWithNavigationsRepository.UpdateAsync(entity);
});
Expand All @@ -199,6 +208,7 @@ await WithUnitOfWorkAsync(async () =>
await WithUnitOfWorkAsync(async () =>
{
var entity = await AppEntityWithNavigationsRepository.GetAsync(entityId);
await unitOfWorkManager.Current.SaveChangesAsync();
entity.OneToMany = new List<AppEntityWithNavigationChildOneToMany>()
{
new AppEntityWithNavigationChildOneToMany
Expand Down Expand Up @@ -235,6 +245,7 @@ await WithUnitOfWorkAsync(async () =>
await WithUnitOfWorkAsync(async () =>
{
var entity = await AppEntityWithNavigationsRepository.GetAsync(entityId);
await unitOfWorkManager.Current.SaveChangesAsync();
entity.OneToMany[0].ChildName = "ChildName2";
await AppEntityWithNavigationsRepository.UpdateAsync(entity);
});
Expand All @@ -249,6 +260,7 @@ await WithUnitOfWorkAsync(async () =>
await WithUnitOfWorkAsync(async () =>
{
var entity = await AppEntityWithNavigationsRepository.GetAsync(entityId);
await unitOfWorkManager.Current.SaveChangesAsync();
entity.OneToMany[0].OneToMany[0].ChildName = "OneToMany-ChildName2";
await AppEntityWithNavigationsRepository.UpdateAsync(entity);
});
Expand All @@ -266,6 +278,7 @@ await WithUnitOfWorkAsync(async () =>
await WithUnitOfWorkAsync(async () =>
{
var entity = await AppEntityWithNavigationsRepository.GetAsync(entityId);
await unitOfWorkManager.Current.SaveChangesAsync();
entity.OneToMany.Clear();
await AppEntityWithNavigationsRepository.UpdateAsync(entity);
});
Expand All @@ -277,6 +290,7 @@ await WithUnitOfWorkAsync(async () =>
await WithUnitOfWorkAsync(async () =>
{
var entity = await AppEntityWithNavigationsRepository.GetAsync(entityId);
await unitOfWorkManager.Current.SaveChangesAsync();
entity.ManyToMany = new List<AppEntityWithNavigationChildManyToMany>()
{
new AppEntityWithNavigationChildManyToMany
Expand All @@ -293,6 +307,7 @@ await WithUnitOfWorkAsync(async () =>
await WithUnitOfWorkAsync(async () =>
{
var entity = await AppEntityWithNavigationsRepository.GetAsync(entityId);
await unitOfWorkManager.Current.SaveChangesAsync();
entity.ManyToMany[0].ChildName = "ChildName2";
await AppEntityWithNavigationsRepository.UpdateAsync(entity);
});
Expand All @@ -303,6 +318,7 @@ await WithUnitOfWorkAsync(async () =>
await WithUnitOfWorkAsync(async () =>
{
var entity = await AppEntityWithNavigationsRepository.GetAsync(entityId);
await unitOfWorkManager.Current.SaveChangesAsync();
entity.ManyToMany.Clear();
await AppEntityWithNavigationsRepository.UpdateAsync(entity);
});
Expand All @@ -313,7 +329,7 @@ await WithUnitOfWorkAsync(async () =>
[Fact]
public async Task Should_Trigger_Domain_Events_For_Aggregate_Root_When_EnsureCollectionLoaded_Navigation_Changes_Tests()
{
_loadEntityWithDetails = true;
_loadEntityWithoutDetails = true;

var entityId = Guid.NewGuid();

Expand Down

0 comments on commit 24f41d3

Please sign in to comment.