EF6 - Rushed load bringing less aggregated data than database data

Asked

Viewed 41 times

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. :

inserir a descrição da imagem aqui

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?

  • You can put your classes ?

  • 1

    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.

  • Updated, I put the generated query as well.

  • Yes, but I need an example. For example, Clienteid 1, Addresses 2 and 3, Documents 4 and 5, Telephones 6, 7 and 8.

  • You want to see the data that is in the database?

  • Yes. Apparently, there’s nothing wrong with the query. It’s optimized, including.

  • What is endereco.CriarEndereco()? What is the need for this?

  • 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?

Show 4 more comments

1 answer

3


All this code is useless:

public ClienteEntity(string nome,
    IList<EnderecoClienteEntity> enderecos)
{
    Nome = nome;

    _enderecos = new List<EnderecoClienteEntity>();
    enderecos.ToList().ForEach(endereco => AddEndereco(endereco));
}

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);
}

Simplify this to:

public virtual ICollection<EnderecoClienteEntity> Enderecos { get; set; }

It is not you who has to manipulate the aggregate entities and levels of protection. It is the Entity Framework that does this. Just let the framework work for you and don’t worry so much about code security.

  • I use so class Scope nut that valid the information of the address entity field for example. If it’s all right, I’ll add it to the list in the entity. Another reason I use it is to prevent outside the customer class.Enderecos.Add(address). You think that msm so is not worth keeping?

  • 1

    So you are "stopping from the outside" also the Controller of manipulating property, which is absolutely normal in MVC. It’s not worth keeping.

  • I’m using DDD and tried to apply the concept of rich model I saw in a course. I saw that in the list prop you took the private from the set. Does this only apply to lists or other tbem props? This controller you said handles the property and the mvc or Entity framework?

  • 1

    You need to do urgently my course. Do not use DDD. It will only disturb you.

  • 1

    I will call you there in the face for us talk about the course that now I was curious rsrs. Anyway mto thanks for your help here bro. A hug!

Browser other questions tagged

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