Lists do not load next to the model

1

My model:

public class Grupo: ModelBase
  {
    public Grupo()
    {
      this.Itens = new List<Item>();
    }

    public Grupo(string nome): this()
    {
      this.Nome = nome;
    }

    public string Nome { get; set; }
    public virtual ICollection<Item> Itens { get; set; }

  }

My repository:

public virtual T BuscaPorId(int id)
{
  return _dbSet.Find(id);
}

And my method in my Service class

public Grupo BuscarPorId(int id)
{
  return grupoRepository.BuscaPorId(id);
}

When I call in the controller it comes the lists that are in the "Group"

public ActionResult Editar(int Id)
{
  if (Id == null)
  {
    return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  }
  Grupo grupo = grupoService.BuscarPorId(Id);

But when I click save and step to my Service that calls my method this.BuscarPorId , exactly the same method that I called in the controller, here it does not load the lists, just the data "Name, Id" p>     

asked by anonymous 16.09.2014 / 22:25

1 answer

2

Find does not guarantee load of dependent entities here:

public virtual T BuscaPorId(int id)
{
    return _dbSet.Find(id);
}

Explicitly load items to ensure they return as desired:

public virtual T BuscaPorId(int id)
{
    return _dbSet.Include(g => g.Itens).SingleOrDefault(g => g.Id == id);
}
    
16.09.2014 / 22:43