Want Entity Framework 6.1 eager loading to load only first level -


i not sure approaching wrong way or default behaviour not working way expecting ...

here 2 sample classes ...

public class person {     public string firstname { get; set; }     public string lastname { get; set; }      public department department { get; set; } } 

second 1 department

public class department {     public string name { get; set; }      public list<person> people { get; set; } } 

context configuration

public mydbcontext() : base("defaultconnection")  {     this.configuration.proxycreationenabled = false;     this.configuration.lazyloadingenabled = false;  }  public dbset<person> people { get; set; } public dbset<department> departments { get; set; } 

i try load people last name 'smith'

var foundpeople          = context               .people               .where(p => p.lastname == "smith"); 

above query load foundpeople firstname , lastname no department object. correct behaviour lazyloading off. , expected well.

now in query eager loading department,

var foundpeople          = context               .people               .where(p => p.lastname == "smith")               .include(p => p.department); 

above query loads foundpeople firstname, lastname, department department->name deparment->people (all people in department, dont want, want load first level of included property.

i dont know intended behaviour or have made mistake.

is there way load first level of included property rather complete graph or levels of included property.

using include() achieve eager loading works if lazy loading enabled on objects--that is, navigation properties must declared virtual, ef proxies can override them lazy-loading behavior. otherwise, eagerly load automatically , include() have no effect.

once declare person.department , department.people virtual properties, code should work expected.

very sorry, original answer wholly incorrect in main. didn't read question closely enough , incorrect in fact on eager behavior. not sure thinking (or upvoted?). real answer below fold:


using example model posted (with necessary modifications: keys entities , removed "this" context constructor) unable exactly reproduce issue. don't think it's doing think it's doing.

when eagerly load department (or explicitly load, using context.entry(...).reference(...).load()) inspect results more closely: there elements in department.people collections, not all persons, only persons loaded in query itself. think you'll find, on last snippet, !foundpeople.selectmany(p => p.department.people).any(p => p.lastname != "smith") == true. is, none of them not "smith".

i don't think there's way around this. entity framework isn't explicitly or eagerly loading people collections (you include(p => p.department.people) that). it's linking ones loaded related object, because of circular relationship in model. further, if there multiple queries on same context load other persons, linked object graph.

(an aside: in simplified case, proxy-creation , lazy-loading configurations superfluous--neither enabled on entities virtue of fact neither have lazy or proxy-able (virtual) properties--the 1 thing did right first time around.)


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -