c# - Entity Framework Check virtual Lists after disposing of ObjectContext -
my scenario have object foo
has virtual list<bar>
property on it. being auto generated ef.
after load foo
dispose of data context, turning foo
business object through dto. example
var newfoo = foo_dto.change(foo);
inside of foo_dto.change
want check if virtual list property empty/null. i understand closing objectcontext , checking navigation property throw error. in data layer there times when return foo
list , foo
without list.
my question how check navigation property see if list has been populated or not , avoid objectcontext error generating
thank much!!
edit
from comments section, purposely want context closed before check see if loaded list<bar>
property.
no, can't, other ugly way of trying , catching exception. can determine whether collection loaded getting owner's dbentityentry
, can obtain through context instance.
but if know front collection may addressed outside scope of context, need load while context alive, or not load , prevent lazy loading. should never allow lazy loading occur outside lifespan of context.
in cases means you'll have turn off lazy loading , eagerly load data required consuming method.
the more work ef in disconnected fashion less allow lazy loading. i'm close considering lazy loading anti-pattern.
Comments
Post a Comment