0

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.

Dan
  • 1,518
  • 5
  • 20
  • 48
  • please include more code in a actual code block currently it is very hard to follow what you are trying to do – Scott Chamberlain Jun 07 '16 at 22:14
  • what is the type of Model.PostOrcamentoServicoProposta? if IQueryable, this code will result in lazy loading and a query to the database, cast it to a List in this case. – DevilSuichiro Jun 07 '16 at 22:21
  • @DevilSuichiro Model.PostOrcamentoServicoProposta is ICollection. This object is already loaded – Dan Jun 07 '16 at 22:24
  • @ScottChamberlain is better now? – Dan Jun 07 '16 at 22:32
  • This will not answer your question but... My thoughts would be never have a list that can be null (it doesn't really make sense IMHO)... I would always instantiate the list within the parent class and just look to see if there is PosOrcSerProStatus.Any(). And one other thing... you can do this db.PostOrcamentoServico.Include("PostOrcamentoServicoProposta").FirstOrDefault(o => o.PosOrcSerId == id); instead of db.PostOrcamentoServico.Include("PostOrcamentoServicoProposta").Where(o => o.PosOrcSerId == id).FirstOrDefault(); – Gwasshoppa Jun 07 '16 at 23:44
  • I notice also that you are using LazyLoading... you may want to check out this post http://stackoverflow.com/questions/18398356/how-to-solve-the-error-the-objectcontext-instance-has-been-disposed-and-can-no-l – Gwasshoppa Jun 07 '16 at 23:52
  • @Gwasshoppa the list will never return null, but, in one object of the list, may have be null. – Dan Jun 08 '16 at 01:08
  • @Gwasshoppa the problem isn't with preload.. as you can see in my code, I'm loading what is necessary (look the Include line) – Dan Jun 08 '16 at 01:12
  • Guys, what I'm talking about is that: I receive a list with object, but, one property of the object may be null, and if I try to put this property (PosOrcSerProStatus ) as filter, I get that error. – Dan Jun 08 '16 at 01:14
  • 1
    This is going nowhere without seeing the relevant code (classes, mappings maybe). `Nullable` is an impossible type. – Gert Arnold Jun 08 '16 at 21:50

1 Answers1

0

I think your problem is that PosOrcSerProStatus is a child relationship and Nullable<Enumerable> does not make sense to entity framework. If entity framework wants to do a .Include(), it tries to instantiate it and can't because it is a null. Just try creating PosOrcSerProStatus as virtual like this and see if it is solved.

// Instantiate your class and at the same time make PosOrcSerProStatus a new list
public PostOrcamentoServicoProposta()
{
  PosOrcSerProStatuses = new List<PosOrcSerProStatus>();
}

// Also make PosOrcSerProStatuses a virtual ICollection
public virtual ICollection<PosOrcSerProStatus> PosOrcSerProStatuses { get; set; }

The only other thing I can think of is that you are closing the DbContext before executing the query within the View...i.e. you are using a using() in your ActionResult and when it gets to the view and you try to do your FirstOrDefault() query the DbContext is closed already.

Gwasshoppa
  • 884
  • 12
  • 18