首页 > 解决方案 > 如何在 ASP.NET Core 中加载具有多对多关系的数据?

问题描述

我正在我的 ASP.NET Core 应用程序中处理多对多关系。从在线阅读资源中,我了解到这还没有得到官方支持,因此需要一个中间类来使其正常工作。

我遇到的问题是,一旦我创建了“多对多”关系,我不知道如何最好地在我的视图中显示数据,并且我正在努力使用这种特定设置遍历所有内容。

在我的示例中,有两张表PartSupplier一张Part可以有很多Supplier,就像一张Supplier可以有很多一样Part

我做的第一件事是创建我的两个实体类PartSupplier

部件.cs

public class Part
{
    public int PartId { get; set; }
    public string PartName { get; set; }
    public string PartNumber { get; set; }
    public string ShortDescription { get; set; }
    public string LongDescription { get; set; }
    public string Category { get; set; }

    //Intermediate entity
    public IList<PartSupplier> PartSupplier { get; set; }
}

供应商.cs

public class Supplier
{
    public int SupplierId { get; set; }
    public string SupplierName { get; set; }
    public string Website { get; set; }
    public int? Telephone { get; set; }
    public string Email { get; set; }

    //Intermediate entity
    public IList<PartSupplier> PartSupplier { get; set; }        
}

你会从上面的代码中看到,我放置了一个中间表,它创建了这个多对多关系,称为PartSupplier

PartSupplier.cs

public class PartSupplier
{
    public int PartId { get; set; }
    public Part Part { get; set; }

    public int SupplierId { get; set; }
    public Supplier Supplier { get; set; }
}

此时,我转到我的数据上下文并覆盖代码的模型构建部分以利用 Fluent API。

上下文.cs

protected override void OnModelCreating(ModelBuilder builder)
{            
    builder.Entity<PartSupplier>().HasKey(ps => new { ps.PartId, ps.SupplierId });
    base.OnModelCreating(builder);
}
public DbSet<Part> Parts { get; set; }
public DbSet<Supplier> Suppliers { get; set; }
public DbSet<PartSupplier> PartSuppliers { get; set; }

好的,到目前为止一切顺利。现在我需要在一个视图中显示此信息,特别是在此视图中,我想显示所有零件,并在它们旁边显示可以购买该零件的所有可用供应商。

我以以下方式加载数据,但这是我有点不确定应该如何构建查询的地方。我应该打电话给中介表还是打电话Part然后包括Supplier?我不确定

PartController.cs

public IActionResult Index()
{
    var data = _context.Part.Include(p => p.Supplier);
    return View(data);
}

索引.cshtml

@model IEnumerable<Part>

<div class="container">
    <div class="row">
        <div class="col">
            <h2>Parts</h2>
            <a asp-action="Create" asp-controller="Parts">Add Part</a>
            <table class="table">
                @foreach (var part in Model)
                {
                <tr>
                    <td>
                        <a asp-action="Edit" asp-route-id="@part.PartId" asp-controller="Part">@part.PartName</a>
                    </td>
                    <td>
                        @part.PartNumber
                    </td>
                    <td>
                        @part.ShortDescription
                    </td>
                    <td>
                       ...I'd like to show the suppliers here...
                    </td>
                </tr>
                }
            </table>
        </div>
    </div>
</div>

我想我几乎已经做到了,但我真的很想得到一些帮助来克服这最后的障碍。非常感谢

标签: entity-frameworkasp.net-coremany-to-manyfluent

解决方案


缺少级联关系Index.cshtml,需要添加ThenInclude

public IActionResult Index()
    {
        var data = _context.Parts.Include(x => x.PartSupplier)
            .ThenInclude(y=>y.Supplier);
        return View(data);
    }

然后在 Index.cshtml 中。

<table class="table">
            @foreach (var part in Model)
            {
                <tr>
                    <td>
                        <a asp-action="Edit" asp-route-id="@part.PartId" asp-controller="Part">@part.PartName</a>
                    </td>
                    <td>
                        @part.PartNumber
                    </td>
                    <td>
                        @part.ShortDescription
                    </td>
                    <td>
                        <ul>
                   @* Here has been changed. *@
                            @foreach(var supplier in part.PartSupplier)
                            {
                                <li>@supplier.Supplier.SupplierName @supplier.Supplier.Website</li>
                            }
                        </ul>
                    </td>
                </tr>
            }
        </table>

在数据库中,我插入了一些数据,例如PartPartSupplier

在此处输入图像描述 在此处输入图像描述

结果。

在此处输入图像描述


推荐阅读