Include all child objects (navigation properies) in the Entity search

Asked

Viewed 66 times

2

I wonder if it is possible to search the object completely filled without calling the include specifying each child object (with their respective children) of my class.

Currently, I’m doing so:

dbContext.Ocorrencia.Include("Pessoa")
                    .Include("Endereco")
                    .Include("Veiculo")
                    .Where(c => c.id == @id).ToList();

In this case, each child object will also have its own child object, generating a cascade. So I’m not finding this way to go including item by item elegant.

There is a way to insert all "children" into a single command. As if there were one .IncludeAll() for example.

I didn’t find any reference that would suit me well.

  • There is an answer to your question at this link: https://codereview.stackexchange.com/questions/30839/dbsett-includeall-method?answertab=oldest#tab-top

2 answers

1

I would just like to comment on something unpretentiously. When we do a bank search, we should always return the smallest possible scope and with only the necessary information for that screen or method. There is really need of a select * from table and still include everything of all children?

If you don’t have control over the recursive amount of possible children, you may have the n+1 problem, a kind of infinite loop. It is precisely for this type of paradigm that the lazyload should be used with much discretion.

I don’t know your scenario, but in a simpler logical model, I would go to the bank only to seek the necessary information and only when it is really necessary.

0

There is no IncludeAll(), however the ObjectQuery<T>.Include is an all include, so if you put a ObjectQuery.Include(Pessoa.Sexo) you will be carrying both Person and Sex at the same time.

Another approach would be to activate Lazy Loading. Lazy Load will fetch all related entities. The problem is that it can carry more things than you want, being a problem for serialization, mainly using obsolete frameworks. I do not recommend this approach, because apparently the same is disabled in your project so someone made this decision and it would be unwise to activate without understanding the reasons for this decision.

To activate Lazy Load, remove the virtual from the property that represents the relationship and leave the Lazyloadingenabled equal to true.

Browser other questions tagged

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