Why does EF Core not load the information from the related Tables?

Asked

Viewed 102 times

0

I will start by posting my data templates

public class Anuncio
{
    public int Id { get; set; }
    [Required]
    public string Descricao { get; set; }
    [Required]
    public TipoAnuncio TipoAnuncio { get; set; }
    [Required]
    public bool Aprovado { get; set; }
    [Required]
    public DateTime DataPublicacao { get; set; }
    [Required]
    public virtual ICollection<Imagem> Imagens { get; set; }
    [Required]
    public int Telefone { get; set; }
    [EmailAddress]
    public string Email { get; set; }
    [Required]
    public Pais Pais { get; set; }

    [Required]
    public Localidade Localidade { get; set; }

    public ApplicationUser User { get; set; }

}

public class TipoAnuncio
{
    public int Id { get; set; }
    [Required]
    public string Nome { get; set; }
}
public class TipoAnuncio
{
    public int Id { get; set; }
    [Required]
    public string Nome { get; set; }
}

When I consult the data in the database the data exists what excludes the possibility that the data does not exist even in the database

Where there is trouble? When I consult advertise there are data that are not coming and the data are all coming by means of another table.

inserir a descrição da imagem aqui

How I’m trying to get that data?

    var anuncios = await _context.Anuncio.ToListAsync();

Here are the data in the database inserir a descrição da imagem aqui

  • 1

    If you connected correctly, and have it loaded for example by the method Include should appear the relationship data. Now as you have not posted the relations it becomes complicated to say

  • Exactly this was the problem @Virgilionovic was not using the Include method

1 answer

0


The answer is simple:

I need to use Include to all other data tables.

var   anuncios = await _context.Anuncio.Include(x => x.Cor)
               .Include(x => x.Imagens)
               .Include(x => x.Localidade)
               .Include(x => x.Pais)
               .Include(x => x.Porte)
               .Include(x => x.Raca)
               .Include(x => x.Sexo)
               .Include(x => x.TipoAnimal)
               .Include(x => x.TipoAnuncio)
               .Include(x => x.User)
               .Include(x => x.Vacinado)
               .Include(x => x.Cor).ToListAsync();

Browser other questions tagged

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