Reputation: 11
I am trying to load a reward with it's information. I am using the following code:
var rewards = await ctx.RewardGroupRewards.Where(r => r.CampaignRewardGroup.Id == rewardGroupId)
.Include(r => r.CampaignRewardGroup)
.Include(r => r.CampaignRewardCategory)
.Include(r => r.CostDenomination)
.Include(r => r.Reward)
.ToListAsync();
When I inspect the result in the debugger, the first item in the list is loaded correctly but the second item in the list has been loaded with what you'd get if you created a new group, even though its group Id is set correctly (or it wouldn't appear in the results at all).
Upvotes: 0
Views: 48
Reputation: 11
Just in case this is useful for anyone, I found a solution to this.
I installed the EntityFrameworkCore.Proxies package and enabled lazy loading, as described here (https://dotnetcoretutorials.com/2019/09/07/lazy-loading-with-ef-core/). This gave some errors for mapping properties which were not virtual (although not the ones used in the specific query above). Once I'd fixed that, everything worked.
The weird thing is, it now works with lazy loading off as well so it could be enough to just add the Proxies reference to the project or make sure all your navigation properties are virtualised, but I'm going to leave LL on anyway as it makes sense for my project.
Upvotes: 1