JDalri
JDalri

Reputation: 388

Entity framework retrieve all data for a model that has many many-to-many relationships

I have a principal model Produto, and this model has some many-to-many relationships to other models.

e.g.: One Produto can have many Aplicacoes and one Aplicacao can belong to many Produtos;
One Produto can have many Opcionais and one Opcional can belong to many Produtos;

I want to display all my Produto in the Index view, displaing they related Aplicacao and Opcional as well.

My problem is to retrieve all this data. When I query only the Produto with it's Aplicacao, it works, but I don't know how to join the related Opcional in it.

Produto

public class Produto
{
    public int Id { get; set; }
    public string Nome { get; set; }
    public string Descricao { get; set; }
    public ICollection<Aplicacao> Aplicacoes { get; set; }
    public ICollection<Opcional> Opcionais { get; set; }
}

Aplicacao

public class Aplicacao
{
    public int Id { get; set; }
    public string Nome { get; set; }
    public ICollection<Produto> Produtos { get; set; }
}

Opcional

public class Opcional
{
    public int Id { get; set; }
    public string Descricao { get; set; }
    public ICollection<Produto> Produtos { get; set; }
}

Contexto

public DbSet<Produto> Produtos { get; set; }
public DbSet<Aplicacao> Aplicacoes { get; set; }
public DbSet<Opcional> Opcionais { get; set; }

modelBuilder.Entity<Produto>()
            .HasMany(c => c.Aplicacoes)
            .WithMany(p => p.Produtos)
            .Map(
                m =>
                     {
                         m.MapLeftKey("ProdutoId");
                         m.MapRightKey("AplicacaoId");
                         m.ToTable("ProdutoAplicacao");
                     });

        modelBuilder.Entity<Produto>()
            .HasMany(c => c.Opcionais)
            .WithMany(p => p.Produtos)
            .Map(
                m =>
                {
                    m.MapLeftKey("ProdutoId");
                    m.MapRightKey("OpcionalId");
                    m.ToTable("ProdutoOpcional");
                });

I want to know how can I query all Produto and they related Aplicacao and Opcional from DB so I can display them in my Index view?

Upvotes: 0

Views: 444

Answers (1)

James Dev
James Dev

Reputation: 3009

You can eager load the related entities with the linq to Entity keyworkd Include(). This will load up the Producto and all related Aplicacoes and Opcionais:

var productosList = MyDbContext.Produtos.Include(x => x.Aplicacoes)
                                        .Include(x => x.Opcionais).ToList();

Or alternatively lazy load the related entities by decorating your entity class with the keyword virtual:

public class Produto
{
    public int Id { get; set; }
    public string Nome { get; set; }
    public string Descricao { get; set; }
    public virtual ICollection<Aplicacao> Aplicacoes { get; set; }
    public virtual ICollection<Opcional> Opcionais { get; set; }
}

Upvotes: 2

Related Questions