c# - WPF+EF - The operation cannot be completed because the DbContext has been disposed -
creating wpf app entity framework 6. have view model list of categories , list of items (in selected category). when selectedcategory changes, i'm using following method fill list of items:
currentlist = new observablecollection<t>(context.set<t>().where(p => p.isgroup == false).where(m => m.parentid == _currentcategory.id));
i'm mot showing different error handling etc. open form, select categories, , works perfect. if close , open same form few times, exception: "the operation cannot completed because dbcontext has been disposed". after 2nd open, - after 3rd or 4th access same form. context disposed when form closes.
what wrong , why exception raises not regularly , never on first opening?
------upd 1------ ok, i'm showing more code i'm sure problem in row.
form creation:
context = new entities(); context.set<t>().load(); loadcurrentlist();
loadcurrentlist():
if ((currentcategory == null) || (currentcategory.id == 0)) { currentlist = new observablecollection<t>(context.set<t>().local.where(p => p.isgroup == false).where(m => m.parent == null)); } else { currentlist = new observablecollection<t>(context.set<t>().local.where(p => p.isgroup == false).where(m => m.parentid == currentcategory.id)); }
form closing command:
context.dispose();
currentcategory property set method raises loadcurrentlist() method
that's all! adding tolist() or using query database (not local) doesn't help. solve i've found create internal list variable, read table on form creation, , make requests internal variable, not dbcontext.
do these additions make sense?
the problem experiencing due linq's deferred execution
.try change query like
currentlist = new observablecollection<t>(context.set<t>().where(p => p.isgroup == false).where(m => m.parentid == _currentcategory.id).tolist());
use .tolist()
method.
Comments
Post a Comment