Lazyload even putting . include(t => t.Model)

Asked

Viewed 81 times

3

I’m having the mistake:

Exception Details: System.Objectdisposedexception: The Objectcontext instance has been disposed and can no longer be used for Operations that require a Connection.

When I make the call:

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

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

I’m doing in 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 Function BolGeral.GerarIDCImpressaoMensal();

    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 the IEnumerable<boleto> clientesFiltrados has a include not to do Lazyload, but because then error as if doing Lazyload, when in the controller I disposed?

1 answer

1


The statement of Include must come before Where. The assembly of IQueryable can go wrong if you ride the Where and then use the 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 customer list in memory before mounting the Where because the Entity Framework tries to solve 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();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.