Browse Source

Merge pull request #19547 from abpframework/AbpEfCoreNavigationHelper-Clear

Clear `AbpEfCoreNavigationHelper` after `SaveChangesAsync`.
pull/19552/head
maliming 2 years ago
committed by GitHub
parent
commit
1e3060bfb5
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. 21
      framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/DomainEvents_Tests.cs

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

@ -181,7 +181,6 @@ public abstract class AbpDbContext<TDbContext> : DbContext, IAbpEfCoreDbContext,
if (entityChangeList != null)
{
EntityHistoryHelper.UpdateChangeList(entityChangeList);
AbpEfCoreNavigationHelper.Clear();
auditLog!.EntityChanges.AddRange(entityChangeList);
Logger.LogDebug($"Added {entityChangeList.Count} entity changes to the current audit log");
}
@ -209,6 +208,7 @@ public abstract class AbpDbContext<TDbContext> : DbContext, IAbpEfCoreDbContext,
finally
{
ChangeTracker.AutoDetectChangesEnabled = true;
AbpEfCoreNavigationHelper.Clear();
}
}

21
framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/DomainEvents_Tests.cs

@ -184,11 +184,19 @@ public abstract class DomainEvents_Tests<TStartupModule> : TestAppTestBase<TStar
await AppEntityWithNavigationsRepository.InsertAsync(new AppEntityWithNavigations(entityId, "TestEntity"));
var entityUpdatedEventTriggered = false;
var personCreatedEventCount = 0;
var entityUpdatedEventTriggerCount = 0;
LocalEventBus.Subscribe<EntityUpdatedEventData<AppEntityWithNavigations>>(data =>
LocalEventBus.Subscribe<EntityCreatedEventData<Person>>(data =>
{
personCreatedEventCount++;
return Task.CompletedTask;
});
LocalEventBus.Subscribe<EntityUpdatedEventData<AppEntityWithNavigations>>(async data =>
{
entityUpdatedEventTriggered = !entityUpdatedEventTriggered;
return Task.CompletedTask;
await PersonRepository.InsertAsync(new Person(Guid.NewGuid(), Guid.NewGuid().ToString(), new Random().Next(1, 100)));
});
// Test with simple property
@ -199,6 +207,7 @@ public abstract class DomainEvents_Tests<TStartupModule> : TestAppTestBase<TStar
await AppEntityWithNavigationsRepository.UpdateAsync(entity);
});
entityUpdatedEventTriggered.ShouldBeTrue();
personCreatedEventCount.ShouldBe(++entityUpdatedEventTriggerCount);
LocalEventBus.Subscribe<EntityUpdatedEventData<AppEntityWithValueObjectAddress>>(data =>
{
@ -214,6 +223,7 @@ public abstract class DomainEvents_Tests<TStartupModule> : TestAppTestBase<TStar
await AppEntityWithNavigationsRepository.UpdateAsync(entity);
});
entityUpdatedEventTriggered.ShouldBeTrue();
personCreatedEventCount.ShouldBe(++entityUpdatedEventTriggerCount);
entityUpdatedEventTriggered = false;
await WithUnitOfWorkAsync(async () =>
@ -223,6 +233,7 @@ public abstract class DomainEvents_Tests<TStartupModule> : TestAppTestBase<TStar
await AppEntityWithNavigationsRepository.UpdateAsync(entity);
});
entityUpdatedEventTriggered.ShouldBeTrue();
personCreatedEventCount.ShouldBe(++entityUpdatedEventTriggerCount);
// Test with one to one
entityUpdatedEventTriggered = false;
@ -236,6 +247,7 @@ public abstract class DomainEvents_Tests<TStartupModule> : TestAppTestBase<TStar
await AppEntityWithNavigationsRepository.UpdateAsync(entity);
});
entityUpdatedEventTriggered.ShouldBeTrue();
personCreatedEventCount.ShouldBe(++entityUpdatedEventTriggerCount);
LocalEventBus.Subscribe<EntityUpdatedEventData<AppEntityWithNavigationChildOneToOne>>(data =>
{
@ -250,6 +262,7 @@ public abstract class DomainEvents_Tests<TStartupModule> : TestAppTestBase<TStar
await AppEntityWithNavigationsRepository.UpdateAsync(entity);
});
entityUpdatedEventTriggered.ShouldBeTrue();
personCreatedEventCount.ShouldBe(++entityUpdatedEventTriggerCount);
// Test with one to many
entityUpdatedEventTriggered = false;
@ -267,6 +280,7 @@ public abstract class DomainEvents_Tests<TStartupModule> : TestAppTestBase<TStar
await AppEntityWithNavigationsRepository.UpdateAsync(entity);
});
entityUpdatedEventTriggered.ShouldBeTrue();
personCreatedEventCount.ShouldBe(++entityUpdatedEventTriggerCount);
LocalEventBus.Subscribe<EntityUpdatedEventData<AppEntityWithNavigationChildOneToMany>>(data =>
{
@ -281,6 +295,7 @@ public abstract class DomainEvents_Tests<TStartupModule> : TestAppTestBase<TStar
await AppEntityWithNavigationsRepository.UpdateAsync(entity);
});
entityUpdatedEventTriggered.ShouldBeTrue();
personCreatedEventCount.ShouldBe(++entityUpdatedEventTriggerCount);
// Test with many to many
entityUpdatedEventTriggered = false;
@ -297,6 +312,7 @@ public abstract class DomainEvents_Tests<TStartupModule> : TestAppTestBase<TStar
await AppEntityWithNavigationsRepository.UpdateAsync(entity);
});
entityUpdatedEventTriggered.ShouldBeTrue();
personCreatedEventCount.ShouldBe(++entityUpdatedEventTriggerCount);
entityUpdatedEventTriggered = false;
await WithUnitOfWorkAsync(async () =>
@ -306,6 +322,7 @@ public abstract class DomainEvents_Tests<TStartupModule> : TestAppTestBase<TStar
await AppEntityWithNavigationsRepository.UpdateAsync(entity);
});
entityUpdatedEventTriggered.ShouldBeTrue();
personCreatedEventCount.ShouldBe(++entityUpdatedEventTriggerCount);
}
private class MyCustomEventData

Loading…
Cancel
Save