c# - Does the eager Include function call data in-memory? -


i reading this answer , realized i'm not clear on how eager loading works in in-memory lifecycle of iqueryable.

say have db.customers.include("orders"), generates object graph this:

customer    order    order    order 

if don't enumerate return include load data in-memory?

that is, does

iqueryable<customer> customerswithorders = db.customers.include("orders"); 

imply customer collection (and orders) has been brought in-memory eagerly includedorders? or, "eagerness" mean if/when customer collection enumerated, orders brought in-memory well?

there's 2 things going on here. first, no query submitted database until operation done requires execution of query. example, if like:

var foos = db.foos.where(...); 

no query has been issued yet. however, if like:

foreach (var foo in foos) {     ... } 

then, query sent database. other things cause query executed things calling tolist(), count(), etc. basically, whenever there's need actual data, then, , entity framework send query.

then, there's totally separate concept of eager vs lazy loading. related items , whether entity framework should issue 1 or more joins part of query or not. using include(), you're instructing entity framework issue join relationship. again, no query issued until evaluated (iteration, enumeration, count, etc.), when evaluated it, both set of entities , related entities included pull @ once.

if choose not include relationship, fallback lazy-loading, means relationship not materialized unless access in way. similar in respects how initial query worked. related items fetched @ point data needed; otherwise no query issued. however, entirely separate query.

long , short, need pay attention data need , when. if you're going utilize related entities, should include before query evaluated, either way, query sent when data represents necessary.


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -