c# - The ObjectContext instance has been disposed and can no longer be used for operations that require a connection -
i'm building application using user controls have main form , display user controls code:
modules.ctrllistcontractors mo = new modules.ctrllistcontractors(); splitcontainercontrol.panel1.controls.clear(); splitcontainercontrol.panel1.controls.add(mo); and within user control can put problem error message :
the objectcontext instance has been disposed , can no longer used operations require connection.
i have relations tables displayed gridview inside ctrllistcontractors , when click menu button show ctrllistcontractors above error message , think it's because lazy loading need execute more queries additional data related tables.
i have code execute @ user control load event:
using (contractorsentities context = new contractorsentities(properties.settings.default.connection)) { memberbindingsource.datasource = context.members.tolist(); } i think problem solved if can data main table , related tables @ same query before objectcontext disposed
if want dispose objectcontext (or dbcontext), you'll have grab all data need first. including related entities.
that can done using include, there 2 overloads: include(string) , more strongly typed include (available ef 4.1)
so, assuming there's relationship member (0-1) ---- (*) contractors, can include collection:
context.members.include(x => x.contractors).tolist(); but said, you'd have include everything need before disposing context. either or don't dispose context , lazily load details.
Comments
Post a Comment