0

Please note that I'm not having problems with the disposed database context and the question is not about how to handle the not-yet-loaded navigational properties.

I've seen two different suggestions on how to handle the exception below.

The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.

The first one, which I've always used recommends Include(item => item.property). However, there's another one and it suggest using ViewBag, which I've never seen before in this context.

My question is whether there are any advantages to using ViewBag or if the Include approach is a generally the best?

Community
  • 1
  • 1
Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438
  • 4
    `ViewBag` is a local storage (simple dictionary variable) in ASP.NET MVC it's not related to EF – Sergey Berezovskiy Oct 15 '15 at 09:11
  • Am I to understand that the second suggestion is just a confused speculation and not the actual answer? I've never used it and I don't feel like wasting time testing it, unless there's a wise reason to. :) – Konrad Viltersten Oct 15 '15 at 09:16
  • 4
    What you linked to isn't a solution or recommendation - it's a bad answer. It won't work unless everything is eagerly loaded. There's no difference between storing the data in a ViewBag or any other variable or property – Panagiotis Kanavos Oct 15 '15 at 09:17
  • 1
    I guess that answer sneaked past the review process because it was added months after the question was asked and it kinda looks like a good answer if you don't know the domain at all. Other than that, it's nonsense and should be removed. – DavidG Oct 15 '15 at 09:20
  • @DavidG there are other, dangerous answers to that question. The linked answer is just wrong. The one about not disposing the context though, is dangerous – Panagiotis Kanavos Oct 15 '15 at 09:24
  • @PanagiotisKanavos Indeed, I'm just looking at them now... – DavidG Oct 15 '15 at 09:25

0 Answers0