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 ?