c# - EF 5.0 Database first, POCO types and Lazy Loading -


i'm not of db guy , i'm pretty new ef. confused erratic behavior of lazy loading on fk relationships.

let's have table parent , table children. children has non-unique fk parent. code:

parent getparent(int parentid) {     return entities.parents.single(p => p.parentid == parentid); }  child getchildbyname(int parentid, string name) {     var parent = getparent(parentid);     return parent.children.single(c => c.name == name);    } 

now, call parent.children.single fail because sequence parent.children empty. not always, sometimes, makes terribly frustrating. when fail have verified via intellitrace no sql call fetch children has executed.

this relationship created correctly in database , input parameters correct. if use include("children") , eagerly load children of course work every time.

if throw thread.sleep(1000) in after parent before filter child, children more not loaded , call succeeds.

so, far can tell, timing related issue. fk relationships not being loaded on demand, being loaded @ seemingly arbitrary times. don't understand behavior , assume must missing something.

i don't want add include("somefk") everywhere makes code more brittle and, really, why should have that? if have sprinkle calls include on place

a) grabbing data don't need b) writing code must edit whenever add or remove relationships c) not getting (aside trivial code generation) out of orm. may writing raw sql calls @ point.

so yeah, must missing something, i've looked , have not been able find comprehensive explanation of lazy loading using poco types (database first!) ef.

i wonder if has entities object have in parent method. entities object disposed or unavailable that, while parent object grabbed, child not because entity disposed before lazy loading can take place?

i'd recommend code pattern objects explicit loading if you're going retrieve them outside of scope of entity context. otherwise context gone, object unable lazy load? not 100% on it's why explicitly load when returning records without dbcontext object available.


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -