简体   繁体   中英

Working with EF 6 MVC 5 and retrieving data from many-to-many relationship

I'm having some troubles in ASP.NET MVC 5 and Entity Framework 6.
My application have PRODUTO and these have APLICACAO.
One PRODUTO can have many APLICACAO and one APLICACAO can belong to many PRODUTO. So, it is a many-to-many ralationship.

Some information:

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; }        
}

Aplicacao

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

In Context I set the DbSets and the Fluent API

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

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

Than EF creates the tables and the bridge table, so far so good.

In my Initializer, I create one Product like so:

var app = context.Aplicacoes.ToList().Find(a => a.Nome == "Industrial");
var app2 = context.Aplicacoes.ToList().Find(a => a.Nome == "Betoneiras");

var produto = new Produto {Nome = "Produto 1", Descricao = "Descrição", Aplicacoes = new List<Aplicacao>{app, app2}};

      context.Produtos.Add(produto);

Then, EF populates the APLICACAO and PRODUTO tables, also it populates the PRODUTOAPLICACAO table.

However when I try to load those informations on my Index view of Product, the Aplicacoes is empty and I can't figure out how to fix this.

ProdutoController

public ActionResult Index()
    {
        return View(db.Produtos.Include(x => x.Aplicacoes.Select(y => y.Produtos)).ToList());
    }

Index view

<table class="table">
<tr>
    <th>
        @Html.DisplayNameFor(model => model.Nome)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.Descricao)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.Aplicacoes)
    </th>
    <th></th>
</tr>
@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Nome)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Descricao)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Aplicacoes)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
            @Html.ActionLink("Details", "Details", new { id=item.Id }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.Id })
        </td>
    </tr>
}
</table>

I need help to fix the query that retrive the model for the view so that I can display the names of each Aplicacao that is related to one product into my Index view.

Thanks

After @Gert Arnold comment that my query was right, i found the "error":

in my view i have included another foreach for the names of the Aplicacao that were retrieved from DB. Now my view its like this:

<table class="table">
<tr>
    <th>
        @Html.DisplayNameFor(model => model.Nome)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.Descricao)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.Aplicacoes)
    </th>
    <th></th>
</tr>
@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Nome)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Descricao)
        </td>
        <td>
            <ul>
                @foreach (var app in item.Aplicacoes)
                {
                    <li>@app.Nome</li>
                }
            </ul>
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
            @Html.ActionLink("Details", "Details", new { id=item.Id }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.Id })
        </td>
    </tr>
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM