entity framework - Do canonical LINQ queries guard against N+1 -


with lazy loading used default, know should call .include() on entity framework entities pull in associated entities want in queries reduce number of calls db if you're calling linq methods on entities. if don't, run risk of repeated database calls each row (the n+1 problem)

can confirm if write canonical linq query, joins defined explicitly, guard against n+1?

from x in _context.tblorder join y in _context.tblcustomer equals y.id = x.customerid select x 

is there way n+1 creep in when we're loading in required entities joins?

edit

as background, asked how junior developers guard against n+1. mentioned simplest way write out queries , define joins, want confirmation indicated 100% accurate.

if asking is

will query hit database once?

then answer yes. linq ef translates expression raw sql , when evaluate query send database e.g. tolist()/foreach/for etc. , once query sent nothing else unless explicitly tell otherwise.

your linq statement simplified using lambda expression e.g.

_context.tblorder.include("customer").tolist(); 

this give order details, including related customer details, in single database trip.


Comments