I know how to fix this problem, but, this error is appearing in a situation totally different to me.
In my MVC Razor view I have the following code that will filter a list of the model, and bring to me a specific object based on the user logged on and also with a field where the result is different than null:
@{
Models.PostOrcamentoServicoProposta proposta = Model.PostOrcamentoServicoProposta.FirstOrDefault(p => p.Usuarios.UsuEmail.Equals(User.Identity.Name, StringComparison.OrdinalIgnoreCase) && p.PosOrcSerProStatus != null);
if (proposta != null)
{
// create a div here
}
}
In my ActionResult I have the following load
using (ServiciliEntities db = new ServiciliEntities())
{
// include data
PostOrcamentoServico orcamento = db.PostOrcamentoServico.Include("PostOrcamentoServicoProposta").Where(o => o.PosOrcSerId == id).FirstOrDefault();
return View(orcamento);
}
As we can see, in my view I receive a model that contains a list of PostOrcamentoServicoProposta and I want to get just one specic PostOrcamentoServicoProposta item according the user logged on.
No problem about that, but I also want the one where PosOrcSerProStatus shouldn't be null.
And there is the problem, this field PosOrcSerProStatus.
The PosOrcSerProStatus field is an Nullable<Enumerable>
and in sometimes it may be possible to be nullable.
So, when I debug my view, I can see that PosOrcSerProStatus is null, and, that's okay to me, however, even if I'm trying to manipulate to get only the one that isn't nullable with the condition p.PosOrcSerProStatus != null, then, I get the error:
The ObjectContext instance has been disposed and can no longer be used for operations that require a connection
First: the list of PosOrcSerProStatus is populated in the action with Include("PostOrcamentoServicoProposta");
Second: Model.PostOrcamentoServicoProposta contains objects;
Third: In all PostOrcamentoServicoProposta objects they're populated properly and PosOrcSerProStatus can be or cannot be null.