Note that there are some explanatory texts on larger screens.

plurals
  1. POCustom Explicit Loading in Entity Framework - any way to do it?
    primarykey
    data
    text
    <p>I've got a list of entity object Individual for an employee survey app - an Individual represents an employee or outside rater. The individual has the parent objects Team and Team.Organization, and the child objects Surveys, Surveys.Responses. Responses, in turn, are related to Questions.</p> <p>So usually, when I want to check the complete information about an Individual, I need to fetch Individuals.Include(Team.Organization).Include(Surveys.Responses.Question).</p> <p>That's obviously a lot of includes, and has a performance cost, so when I fetch a list of Individuals and don't need their related objects, I don't bother with the Includes... but then the user wants to manipulate an Individual. So here's the challenge. I seem to have 3 options, all bad:</p> <p>1) Modify the query that downloads the big list of Individuals to .Include(Team.Organization).Include(Surveys.Responses.Question). This gives it bad performance.</p> <p>2) Individuals.Load(), TeamReference.Load(), OrganizationReference.Load(), Surveys.Load(), (and iterate through the list of Surveys and load their Responses and the Responses' Questions).</p> <p>3) When a user wishes to manipulate an Individual, I drop that reference and fetch a whole brand new Individual from the database by its primary key. This works, but is ugly because it means I have two different kinds of Individuals, and I can never use one in place of the other. It also creates ugly problems if I'm iterating across a list repeatedly, as it's tricky to avoid loading and dropping the fully-included Individuals repeatedly, which is wasteful.</p> <p>Is there any way to say </p> <pre><code>myIndividual.Include("Team.Organization").Include("Surveys.Responses.Question"); </code></pre> <p>with an existing Individual entity, instead of taking approach (3)?</p> <p>That is, is there any middle-ground between "fetch everything from the database up-front" and "late-load one relationship at a time"?</p> <p>Possible solution that I'm hoping I could get insight about:</p> <p>So there's no way to do a manually-implemented explicit load on a navigational-property? No way to have the system interpret</p> <pre><code>Individual.Surveys = from survey in MyEntities.Surveys.Include("Responses.Question") where survey.IndividualID = Individual.ID select survey; //Individual.Surveys is the navigation collection property holding Surveys on the Individual. Individual.Team = from team in MyEntities.Teams.Include("Organization") where team.ID = Individual.TeamID select team; </code></pre> <p>as just loading Individual's related objects from the database instead of being an assignment/update operation? If this means no actual change in X and Y, can I just do that?</p> <p>I want a way to manually implement a lazy or explicit load that isn't doing it a dumb (one relation at a time) way. Really, the Teams and Organizationss aren't the problem, but the Survey.Responses.Questions are a massive buttload of database hits.</p> <p>I'm using 3.5, but for the sake of others (and when my project finally migrates to 4) I'm sure responses relevant to 4 would be appreciated. In that context, similar customization of lazy loading would be good to hear about too.</p> <p>edit: Switched the alphabet soup to my problem domain, edited for clarity.</p> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload