6
I need a help. My problem is this: I cannot return my object items.
Follow my code for analysis.
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;}
}
Class Clienteaddressee
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 Clienteender items return to me Count = 0
:
var clientes = repositorioCliente.ListarTudo();
I know that if I put the in the Include("Clienteendereco") query method it will return all my items, but this method is generic, so I changed the method by passing the required 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 mine Controller (MVC) for all methods I will have to pass the list of necessary includes.
private readonly RepositorioGenerico<Cliente> repositorioCliente = new RepositorioGenerico<Cliente>();
private string[] includes = {"ClienteEndereco","ClienteEndereco.Cidade"};
var Clientes = repositorioCliente.ListaTudo(includes);
Besides thinking my code is getting too polluted I’m having a lot of work to map in all Controllers the Includes necessary, just there in my View not to burst the error of:
"The Objectcontext instance has been disposed and can no longer ...."
There is some other way to bring the items without me needing to use Includes?