Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only remove the changed entities at end of SaveChangesAsync method. #20396

Merged
merged 1 commit into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading