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

0

I was using the DDD Model with Repositories and with 3 layers of data, (with% of%,% with% and% with of%), with the help of some users here I decided to change the project and now I've finished removing 2 layers to Serviço and Repositorio of my project.

I'm still studying on this subject that is becoming clearer to me (now I understand why my code was long-winded, as said by the user @ jbueno), I have my context declared and I'm using the 6.0 version of EntityFramework.

I created a controller in my Dominio layer and did the following search:

 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 did not bring the client,

This is my class serviço :

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

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

My class repositório :

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

LazyLoading is enabled, it loads the Web Correct but the Client gets Cliente .

I was with the DDD model and using MensagemUnidade , my project now has the following structure, a ClienteId layer, a null layer (where my Repositorio is and my layers Dominio , when I changed the project I was in doubt about Infra and things like, now everything is fitting better, but I still have this problem.

    
asked by anonymous 04.11.2016 / 16:44

1 answer

1

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

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

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

An important note about the following sentence

  

LazyLoading is enabled, it loads the correct ClientId but the Client is null.

LazyLoading is not required to use Include . In fact, when using Include you are talking about eager loading and not lazy loading .

In short, the main difference between the two is that using eager loading ( Include , Load ) techniques the entity loads into memory at the time in> is materialized ( see more about it here ), that is, the query that is executed in the database , already contains information about explicit entities (in your case, using the Include method). As long as lazy loading , related entities are just proxies and their query will only be executed in the database when you attempt to access the entity.

Here's an interesting post on the subject: What is Lazy Loading and Eager Loading?

    
04.11.2016 / 18:13