Include all child objects (navigation properies) in entity search

2

I wonder if it is possible to fetch the fully populated object without having to call include by specifying each child object (with their respective children) in my class.

Currently, I'm doing this:

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 looking elegant this way to go including item by item.

  

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

I did not find any reference that suited me well.

    
asked by anonymous 17.03.2017 / 23:08

2 answers

1

I just want to comment something unpretentiously. When we do a search on the bank, we should always return as little scope as possible and with only the necessary information for that screen or method. Do you really need a select * from table and still include everything from all the children?

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

I do not know your scenario, but in a simpler logical model, I would go to the bank just to get the information I need and only when it's really needed.

    
14.11.2017 / 14:32
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 loading both Person and Sex at the same time.

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

To enable Lazy Load, remove the virtual property representing the relationship and leave LazyLoadingEnabled equal to true.

    
14.11.2017 / 14:51