Include EntityFrameWork

3

I'm working with Entity Framework 6, I made all the necessary settings. I have a Person Class that owns a Property of the Type Address, inside address I have a property Municipio that by the end has a property of Type UF. I have a function that I need to retrieve all the people registered and display on a grid, so far so good, but the data of the objects that are part of my person class, is not loaded. I used this code:

 _dbcontext.Pessoas.Include(x => x.Endereco.Municipio.Uf).ToList();
When I do the Project Debug EntityFramework generates the Sql correctly, I copy the sql and execute in the sqlserver, the data is displayed the way I expected, but the information that the Entity brings from the database is not included in the properties of the my person class, just the existing data in the person table and they are loaded.

    
asked by anonymous 27.06.2014 / 21:43

2 answers

3

That is:

public class Pessoa 
{
    [Key]
    public int PessoaId { get; set; }
    public int EnderecoId { get; set; }

    public virtual Endereco Endereco {get; set;}
}

public class Endereco
{
    [Key]
    public int EnderecoId { get; set; }
    public int MunicipioId { get; set; }

    public virtual Municipio Municipio { get; set; }
}

public class Municipio
{
    [Key]
    public int MunicipioId { get; set; }
    public int UfId { get; set; }

    public virtual Uf Uf { get; set; }
}

Switch:

_dbcontext.Pessoas.Include(x => x.Endereco.Municipio.Uf).ToList();

To:

_dbcontext.Pessoas.Include("Endereco.Municipio.Uf").ToList();

Or:

_dbcontext.Pessoas
    .Include(x => x.Endereco)
    .Include(x => x.Endereco.Select(e => e.Municipio))
    .Include(x => x.Endereco.Municipio.Select(m => m.Uf))
    .ToList();
    
27.06.2014 / 21:55
0

try this

_dbcontext.Pessoas.Include("Endereco", "Endereco.Municipio", "Endereco.Municipio.Uf").ToList();
    
22.05.2015 / 17:09