LazyLoad even putting .include (t = t.Model)

3

I'm having the error:

  

Exception Details: System.ObjectDisposedException: The ObjectContext   instance has been disposed and can not be used anymore for operations   that require a connection.

When I call:

@Html.DisplayFor(modelItem => item.Cliente.Url)

This view receives a model called boleto, which contains Customers;

I'm doing the controller :

        using (var BolGeral = new BoletoGeral())
        {
            IEnumerable<boleto> clientesFiltrados = BolGeral.GerarIDCImpressaoMensal();

            return View(clientesFiltrados);
        }
//se não uso o Dispose ele funciona, pois ai ele ainda está conectado e consegue recuperar os valores.

The BolGeral function.

    public IEnumerable<boleto> GerarIDCImpressaoMensal()
    {
        var DataRef = new DateTime(DateTime.Today.Year, DateTime.Today.AddMonths(1).Month, 1);

        var IdClientesSemBoleto = (from cli in db.Clientes
                                   join bol in db.Boletos on cli.ClienteId equals bol.ClienteId
                                   where bol.DataReferencia == DataRef && cli.Status == 4 && cli.Convenio == 0
                                   select cli.ClienteId);

        var clientes = db.Boletos
            .Where(s => !IdClientesSemBoleto.Distinct().Contains(s.ClienteId))
            .Where(i => i.Cliente.Status == 4 && i.Cliente.Convenio == 0);

        //fazendo o distinct
        IEnumerable<boleto> clientesFiltrados = clientes
              .Include(i => i.Cliente) // Aqui está o include
              .GroupBy(customer => customer.ClienteId)
              .Select(group => group.FirstOrDefault()).OrderBy(o => o.Cliente.Url).ToList();

        return clientesFiltrados;
    }

See that IEnumerable<boleto> clientesFiltrados has an include not to do LazyLoad, but why then of the error as if I were doing LazyLoad, when in the controller I make the disposed?

    
asked by anonymous 29.04.2016 / 18:54

1 answer

1

The declaration of Include must come before Where . The assembly of IQueryable may be wrong if you mount Where and then use Include :

var clientes = db.Boletos
        .Include(i => i.Cliente) // Aqui está o include
        .Where(s => !IdClientesSemBoleto.Distinct().Contains(s.ClienteId))
        .Where(i => i.Cliente.Status == 4 && i.Cliente.Convenio == 0);

Also try to materialize the list of clients in memory before mounting Where because the Entity Framework tries to resolve the list in sentence, and this can generate errors:

var listaDeClientesSemBoleto = IdClientesSemBoleto.Distinct();
var clientes = db.Boletos
        .Include(i => i.Cliente) // Aqui está o include
        .Where(s => !listaDeClientesSemBoleto.Contains(s.ClienteId))
        .Where(i => i.Cliente.Status == 4 && i.Cliente.Convenio == 0);

Finally:

var clientesFiltrados = clientes
          .GroupBy(customer => customer.ClienteId)
          .Select(group => group.FirstOrDefault())
          .OrderBy(o => o.Cliente.Url)
          .ToList();
    
29.04.2016 / 20:42