Reputation: 2018
I've been at this for a few hours, so I'm not sure if I'm too tired, or I'm just missing something...
I'm trying to load this entire path of this graph using EF7.0, and I'm not making it past the compiler! Will somebody be kind enough to give me the syntax sugar to Include this path?
Given one UserTopic - I need to load all related entities to Vector!
Upvotes: 1
Views: 396
Reputation: 2018
Ok in my example and question (which I likely could have been worded better). I needed to return a UserTopic, with the object graph shown loaded. So off I went on my merry while, as I have done many times in EF6..
UserTopic queryUT = dbContext.UserTopics.FirstorDefault(ut => ut.UserTopicID = 1).Include(... #FAIL
OK.. another attempt
var queryUT = dbContext.UserTopics
.Where(ut => ut.UserTopicID = 1)
.Include ... #FAIL (start include with a collection
The list goes on... resolution...
public void ProcessUserTopic(dbContext pdbContext, UserTopic pUserTopic)
var queryUT = pdbContext.UserTopics
.Include(lpUserTopic => lpUserTopic.UserTopicInteractions)
.ThenInclude(lpUserTopInteraction => lpUserTopInteraction.Interaction)
.ThenInclude(lpInteraction => lpInteraction.InteractionProfile)
.ThenInclude(lpProfile => lpProfile.ProfileVectors)
.ThenInclude(lpProfileVector => lpProfileVector.Vector)
.FirstOrDefault(lpUserTopic => lpUserTopic.UserTopicID == pUserTopic.UserTopicID)
Note that I could not traverse the object graph by simply passing in the userTopic, and then traverse the graph. I had to go back to the dbContext, include the object graph FIRST, and then filter on the user topic that I had passed in.
In the past, I isolated what I required first, and then went across the object graph (so I relied on lazy loading). If I wanted eager loading, I included AFTER the filter.
In EF7, you must always traverse the object tree FIRST (using multiple .Include/.Then Include statements), then filter. Also, once isolated on one entity, you can't expand the object graph - as it will only return nulls, even though the DB has values (I know, lazy loading is not in EF7). However, I would have thought that I could have traverse the graph, after isolation to just one entity (which I'm pretty sure is a change from EF6).
Upvotes: 1
Reputation: 16983
You chaining the include:
dbContext.UserTopics.Include(x => x.UserTopicProfile).ThenInclude(...)....ThenInclude(x => x.Vector);
Upvotes: 2