Skip to content

Commit

Permalink
Bugfix/find not eagerloading in repository (#137)
Browse files Browse the repository at this point in the history
* Updated find method in EFCoreRepository to eagerload using ObjectContext rather than RepositoryQuery since RepositoryQuery is not used in FindAsync(object primarykey) method.
  • Loading branch information
JasonMWebb authored Sep 12, 2024
1 parent 5f8a667 commit 4aca138
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Build/Build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion Src/RCommon.EfCore/Crud/EFCoreRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,13 @@ public override bool Tracking

public override IEagerLoadableQueryable<TEntity> Include(Expression<Func<TEntity, object>> path)
{
_includableQueryable = RepositoryQuery.Include(path);
_includableQueryable = ObjectContext.Set<TEntity>().Include(path);
return this;
}

public override IEagerLoadableQueryable<TEntity> ThenInclude<TPreviousProperty, TProperty>(Expression<Func<object, TProperty>> path)
{
// TODO: This is likely a bug. The received is incorrect.
_repositoryQuery = _includableQueryable.ThenInclude(path);
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Customer>();

// 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<IGraphRepository<Customer>>();
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);
}

}
}

0 comments on commit 4aca138

Please sign in to comment.