.Include() does not carry child class - Lazyloading C#

Asked

Viewed 137 times

0

I was using the DDD Model with Repositories and with 3 layers of data, (Serviço,Repositorio and Dominio), with the help of some users here I decided to change the project and now I ended up removing 2 layers to serviço and the repositório of my project.

I am still studying on this subject that is becoming increasingly clear to me (now I understand why my code was wordy, as said by @jbueno), I have my context stated and I am using the 6.0 version of Entityframework.

I created a controller in my layer Web and did the following research :

 List<MensagemUnidade> mensagens = this.Contexto.MensagemUnidade
            .Include(c => c.Cliente).Where(l => l.UnidadeId == unidade.UnidadeAtual && l.OrigemId == (int)enumOrigemMensagem.ADMIN)
            .OrderByDescending(l => l.DataEnvio).Skip(mensagemModel.PaginaAtual * 20)
            .Take(20)               
            .ToList();

She didn’t bring the client,

This and my class Cliente:

public class Cliente : Pessoa
{
    public string FacebookFoto { get; set; }

    public ICollection<TokenCliente> TokensCliente { get; set; }
}

My class MensagemUnidade:

 public class MensagemUnidade
  {
     public virtual Cliente Cliente { get; set; }
     public virtual Unidade Unidade { get; set; }
  }

The Lazyloading is activated, it even carries the ClienteId Correct but the Client stays null.

I was with the DDD model and using Repositorio, my project now has the following structure, a layer Dominio, a layer Infra(where is mine contexto and my layers Web/API , when I changed the project I was in doubt about UnitofWorkand things like, now it’s all fitting better, but I still have this problem.

1 answer

2


You just need to make it clear in your relationship class that Cliente is an entity that will be loaded from the key ClienteId. Like Cliente is a class that inherits from Pessoa and who has the PrimaryKey is this second, there is a certain confusion.

public class MensagemUnidade
{
    public int ClienteId { get; set; }

    [ForeignKey("ClienteId")]
    public virtual Cliente Cliente { get; set; }        
 }

An important remark on the following sentence

Lazyloading is activated, it even carries the Correct Clienteid but the Customer is null.

Lazyloading is not required to use the Include. Actually, when you use Include if you’re talking about Eager loading (or advance loading) and not Lazy loading (lazy load).

In short, the main difference between the two is that using Eager loading (Include, Load) the entity is loaded in memory when query is materialized (see more about it here), that is, the query that is executed in the database, already contains the information regarding the explicit entities (in your case, using the method Include). Already in the Lazy loading, the related entities are only proxies and their query will only be executed in the bank when you try to access the entity.

Here is an interesting publication on the subject: What is Lazy Loading and Eager Loading?

Browser other questions tagged

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