5
I’m having the following problem with my relationships in Entity. When I seek for example a Bank Account by Id :
return _databaseContext.Clientes
.Include(x => x.Enderecos)
.Include(x => x.Documentos)
.Include(x => x.Telefones)
.SingleOrDefault(x => x.Id == id);
It has 2 Addresses, 2 Documents and 3 Telephones. But the Entity only brings 1 document, 1 address and 2 phones. :
I can not understand the reason. Even using the Lazyload he does not carry, apart that he is generating a query extremely huge and poorly done with several Unions and Subselects. Anyone have any idea?
I’ll just put some props because the class is huge:
public class ClienteEntity : Entity<int>
{
private IList<EnderecoClienteEntity> _enderecos;
public ClienteEntity(string nome,
IList<EnderecoClienteEntity> enderecos)
{
Nome = nome;
_enderecos = new List<EnderecoClienteEntity>();
enderecos.ToList().ForEach(endereco => AddEndereco(endereco));
}
public string Nome { get; private set; }
public virtual ICollection<EnderecoClienteEntity> Enderecos
{
get { return _enderecos; }
private set { _enderecos = new List<EnderecoClienteEntity>(value); }
}
public void AddEndereco(EnderecoClienteEntity endereco)
{
if(endereco.CriarEndereco())
_enderecos.Add(endereco);
}
}
I figured out here where the bug is, it’s on the list:
public virtual ICollection<EnderecoClienteEntity> Enderecos { get; set; }
// {
// get { return _enderecos; }
// private set { _enderecos = new List<EnderecoClienteEntity>(value); }
// }
If I leave the list like this, it brings everything in the relationship. But what would be the reason?
How are the data in the bank?
– Leonel Sanches da Silva
You can put your classes ?
– novic
Opa, blz Gypsy? It is normal to make the relationship one-to-Many and mapping the Clienteid in the tables Address, Phone and Document. The data is already in the database and even if I run the select that appears in the output when lazyload use is right. But it doesn’t bring all the related objects, only a few.
– allan.egidio
Updated, I put the generated query as well.
– allan.egidio
Yes, but I need an example. For example, Clienteid 1, Addresses 2 and 3, Documents 4 and 5, Telephones 6, 7 and 8.
– Leonel Sanches da Silva
You want to see the data that is in the database?
– allan.egidio
Yes. Apparently, there’s nothing wrong with the query. It’s optimized, including.
– Leonel Sanches da Silva
What is
endereco.CriarEndereco()
? What is the need for this?– Leonel Sanches da Silva
I figured out the reason for the mistake, if I take my get and private set and just put {get; set;} it brings everything from the relationship. It would be able to arrange without using everything public?
– allan.egidio