简体   繁体   English

使用EF 6 MVC 5并从多对多关系中检索数据

[英]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. 我在ASP.NET MVC 5和Entity Framework 6中遇到了一些麻烦。
My application have PRODUTO and these have APLICACAO. 我的应用程序有PRODUTO,而这些应用程序有APLICACAO。
One PRODUTO can have many APLICACAO and one APLICACAO can belong to many PRODUTO. 一个PRODUTO可以具有许多APLICACAO,而一个APLICACAO可以属于许多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 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 在上下文中,我设置了DbSet和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. 到目前为止,EF比EF创建了表和桥表。

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. 然后,EF填充APLICACAO和PRODUTO表,还填充PRODUTOAPLICACAO表。

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. 但是,当我尝试将这些信息加载到“产品”的“索引”视图中时,Aplicacoes是空的,我不知道如何解决此问题。

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. 我需要帮助来修复为视图检索模型的查询,以便可以在索引视图中显示与一种产品相关的每个Aplicacao的名称。

Thanks 谢谢

After @Gert Arnold comment that my query was right, i found the "error": @Gert Arnold评论我的查询正确之后,我发现了“错误”:

in my view i have included another foreach for the names of the Aplicacao that were retrieved from DB. 在我看来,我已经为从数据库中检索到的Aplicacao的名称添加了另一个foreach。 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>
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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