2

Creating WPF app with Entity Framework 6. I have a view model with a list of categories and a list of items (in selected category). When selectedCategory changes, I'm using the following method to fill a 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. So I open a form, select categories, and everything works perfect. If to close and open the same form a few times, then I get an exception: "The operation cannot be completed because the DbContext has been disposed". Sometimes I get it after 2nd open, sometimes - after 3rd or 4th access to the same form. Context is disposed when form closes.

What I do wrong and why this exception raises not regularly and never on the first opening?

------UPD 1------ Ok, I'm showing you more code but I'm sure the problem is in that 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 to database (not to local) doesn't help. The only solve I've found is to create internal List variable, read table on form creation, and than make requests to this internal variable, not to DBContext.

Do these additions make any sense?

4
  • Since you haven't posted any other code, like where you get your context from, when you dispose it, etc. it is impossible to say why this is happening. Jun 27, 2014 at 9:50
  • Please add some more code for explaination. Jun 27, 2014 at 9:50
  • 2
    We are not magician so please show your code first..
    – Rahul
    Jun 27, 2014 at 9:51
  • Sorry. Wanted to be laconic. Tried to add some code if this helps..
    – EugeneK
    Jun 27, 2014 at 10:28

2 Answers 2

2

The problem you are experiencing is due to LINQ's deferred execution.Try to change your query like

CurrentList = new ObservableCollection<T>(Context.Set<T>().Where(p => p.IsGroup == false).Where(m => m.ParentId == _currentCategory.Id).ToList());

Use .ToList() method.

1
  • Yes, I saw this answer through search. Here it doesn't help. (
    – EugeneK
    Jun 27, 2014 at 10:29
0

You may want to switch around your code a bit. Try using a "using" block to wrap EF context access. For one this automatically disposes on exceptions and other than that it'll be released as soon as you don't need it anymore.

This way you also won't have anyone dispose your DbContext properties through reflection (which depending on your project may also be happening).

1
  • Yes, I'm also thinking about this. But on EF doc site there is advice to make one context for one form and keep it alive while form is in use. So I try to following this cause it makes other things easier like adding, editing, cancelling changes etc.
    – EugeneK
    Jun 27, 2014 at 10:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.