EntityFramework 6 + LazyLoadingEnabled + using ()

6

I need a help. My problem is this: I can not return the items of my object.

Follow my code for review.

Client Class

public partial class Cliente
{
    public Cliente()
    {
        this.ClienteEndereco = new HashSet<ClienteEndereco>();
    }

    public int IdCliente { get; set; }
    public string Nome { get; set; }

    public virtual ICollection<ClienteEndereco> ClienteEndereco { get; set;}
}

Classe ClienteEndereco

public partial class ClienteEndereco
{
    public int IdClienteEndereco { get; set; }
    public int IdCliente { get; set; }
    public int IdCidade { get; set; }
    public string Endereco { get; set; }

    public virtual Cidade Cidade { get; set; }
    public virtual Cliente Cliente { get; set; }
}

Consultation method

    public IList<T> ListarTudo()
    {
        using (MeuContext context = new MeuContext())
        {
            context.Configuration.LazyLoadingEnabled = false;

            return context.Set<T>().ToList();
        }
    }

When I call my query the items in the ClientEnder return me Count = 0 :

var clientes = repositorioCliente.ListarTudo();

I know that if I put the Include query method ("ClientEndereco") it will return all my items, however this method is generic, so I changed the method by passing the necessary includes.

    public List<T> ListaTudo(string[] includes)
    {
        using (MeuContext context = new MeuContext())
        {
            IQueryable<T> query = context.Set<T>();
            if (includes != null)
                foreach (var includeProperty in includes)
                {
                    query = query.Include(includeProperty);
                }
            return query.ToList();
        }
    }

But now, every call I make there in my Controller (MVC) for all methods I'll have to go through the list of necessary includes.

private readonly RepositorioGenerico<Cliente> repositorioCliente = new RepositorioGenerico<Cliente>();

private string[] includes = {"ClienteEndereco","ClienteEndereco.Cidade"};

var Clientes = repositorioCliente.ListaTudo(includes);

In addition to finding that my code is getting very polluted I'm having a lot of work mapping to all the Controllers Requires, just there in my View do not pop the error:

  

"The ObjectContext instance has been disposed and can no longer ...."

Is there any other way to bring items without needing to use Includes ?

    
asked by anonymous 22.05.2015 / 16:55

1 answer

4

If you are having too much work, it means that you are using the wrong tool, and this is evident from your explanation.

By the way, I can not get enough of saying Entity Framework jé is a repository, so you do not have to deploy another repository . Here are the answers I've already given:

About Model Cliente

This does not need to be done. The Entity Framework controls the navigation properties automatically, so nothing needs to be initialized. The part below can be taken from the code:

public Cliente()
{
    this.ClienteEndereco = new HashSet<ClienteEndereco>();
}

About the Query Method

There is no reason for you to disable Lazy Load at this time. It is only justified if you have a very large mass of records to use, which is hardly the case.

In addition, this construction here:

return context.Set<T>().ToList();

Make the Entity Framework perform a FULL TABLE SCAN in your database. The correct one is to mount the sentence in DbSet and only use .ToList() with the proper parameterization. That is, this construct is awful for performance.

About the problem of Include

The problems you are experiencing are caused by the urge to use a generic repository where you do not need to.

This type of construction is completely unnecessary and should be abandoned:

public List<T> ListaTudo(string[] includes)
{
    using (MeuContext context = new MeuContext())
    {
        IQueryable<T> query = context.Set<T>();
        if (includes != null)
            foreach (var includeProperty in includes)
            {
                query = query.Include(includeProperty);
            }
        return query.ToList();
    }
}

If I need to list everything, such as clients, I can just do:

var clientes = context.Clientes.ToList();

If I need to load client data and I want to load it with JOIN addresses, I can do this:

var clientes = context.Clientes.Include(c => c.ClienteEndereco).ToList();

That is, there is no reason to continue a generic repository deployment . Simply use the Entity Framework as the tutorials show that data optimization occurs naturally in almost all cases.

For other cases, ask here on the site.

    
22.05.2015 / 19:03