From 4aca13881615b1b908dd7e6a780cc3359ad52eed Mon Sep 17 00:00:00 2001 From: Jason Webb <46631010+JasonMWebb@users.noreply.github.com> Date: Thu, 12 Sep 2024 10:21:27 -0600 Subject: [PATCH] Bugfix/find not eagerloading in repository (#137) * Updated find method in EFCoreRepository to eagerload using ObjectContext rather than RepositoryQuery since RepositoryQuery is not used in FindAsync(object primarykey) method. --- Build/Build.cs | 2 +- Src/RCommon.EfCore/Crud/EFCoreRepository.cs | 3 +- .../EFCoreRepositoryIntegrationTests.cs | 29 +++++++++++++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/Build/Build.cs b/Build/Build.cs index 101185e6..cbde9cd6 100644 --- a/Build/Build.cs +++ b/Build/Build.cs @@ -135,7 +135,7 @@ protected override void OnBuildInitialized() { Log.Information("Generating NuGet packages for projects in solution"); int commitNum = 0; - string NuGetVersionCustom = "2.0.0.884"; + string NuGetVersionCustom = "2.0.0.885"; //if it's not a tagged release - append the commit number to the package version diff --git a/Src/RCommon.EfCore/Crud/EFCoreRepository.cs b/Src/RCommon.EfCore/Crud/EFCoreRepository.cs index cec2eca2..ea9b7cb1 100644 --- a/Src/RCommon.EfCore/Crud/EFCoreRepository.cs +++ b/Src/RCommon.EfCore/Crud/EFCoreRepository.cs @@ -90,12 +90,13 @@ public override bool Tracking public override IEagerLoadableQueryable Include(Expression> path) { - _includableQueryable = RepositoryQuery.Include(path); + _includableQueryable = ObjectContext.Set().Include(path); return this; } public override IEagerLoadableQueryable ThenInclude(Expression> path) { + // TODO: This is likely a bug. The received is incorrect. _repositoryQuery = _includableQueryable.ThenInclude(path); return this; } diff --git a/Tests/RCommon.Persistence.EFCore.Tests/EFCoreRepositoryIntegrationTests.cs b/Tests/RCommon.Persistence.EFCore.Tests/EFCoreRepositoryIntegrationTests.cs index 37a725c5..3140d407 100644 --- a/Tests/RCommon.Persistence.EFCore.Tests/EFCoreRepositoryIntegrationTests.cs +++ b/Tests/RCommon.Persistence.EFCore.Tests/EFCoreRepositoryIntegrationTests.cs @@ -748,5 +748,34 @@ public async Task Can_Eager_Load_Repository_And_Query_Async() Assert.That(savedCustomer.Orders.Count == 10); } + [Test] + public async Task Can_Eager_Load_Repository_And_Find_Async() + { + var testData = new List(); + + // Generate Test Data + var repo = new TestRepository(this.ServiceProvider); + repo.PersistSeedData(testData); + + var customer = TestDataActions.CreateCustomerStub(); + for (int i = 0; i < 10; i++) + { + var order = TestDataActions.CreateOrderStub(x => x.Customer = customer); + customer.Orders.Add(order); + testData.Add(customer); + } + repo.PersistSeedData(testData); + + var customerRepo = this.ServiceProvider.GetService>(); + customerRepo.Include(x => x.Orders); + var savedCustomer = await customerRepo + .FindAsync(customer.Id); + + Assert.That(savedCustomer != null); + Assert.That(savedCustomer.Id == customer.Id); + Assert.That(savedCustomer.Orders != null); + Assert.That(savedCustomer.Orders.Count == 10); + } + } }