Help me understand entity framework 4 caching for lazy loading -
i getting unexpected behaviour entity framework 4.0 , hoping can me understand this. using northwind database purposes of question. using default code generator (not poco or self tracking). expecting anytime query context framework make round trip if have not fetched objects. behaviour if turn off lazy loading. in application breifly turning on lazy loading , turning off can desired behaviour. pretty sucks, please help. here code example can demonstrate problem.
public sub manyroundtrips() context.contextoptions.lazyloadingenabled = true dim employees list(of employee) = context.employees.execute(system.data.objects.mergeoption.appendonly).tolist() 'makes unnessesary round trip database, loaded employees' messagebox.show(context.employees.where(function(x) x.employeeid < 10).tolist().count) context.orders.execute(system.data.objects.mergeoption.appendonly) each emp employee in employees 'makes unnessesary trip database every time despite orders being pre loaded.' dim integer = emp.orders.count next end sub public sub oneroundtrip() context.contextoptions.lazyloadingenabled = true dim employees list(of employee) = context.employees.include("orders").execute(system.data.objects.mergeoption.appendonly).tolist() messagebox.show(employees.where(function(x) x.employeeid < 10).tolist().count) each emp employee in employees dim integer = emp.orders.count next end sub
why first block of code making unnessesary round trips?
your expectation not correct. queries always query db. always. that's because linq converted sql.
to load object context if it's been fetched , db if hasn't, use objectcontext.getobjectbykey().
Comments
Post a Comment