entity-framework - 如何在 ASP.NET Core 中加载具有多对多关系的数据?
问题描述
我正在我的 ASP.NET Core 应用程序中处理多对多关系。从在线阅读资源中,我了解到这还没有得到官方支持,因此需要一个中间类来使其正常工作。
我遇到的问题是,一旦我创建了“多对多”关系,我不知道如何最好地在我的视图中显示数据,并且我正在努力使用这种特定设置遍历所有内容。
在我的示例中,有两张表Part
,Supplier
一张Part
可以有很多Supplier
,就像一张Supplier
可以有很多一样Part
。
我做的第一件事是创建我的两个实体类Part
和Supplier
部件.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>
我想我几乎已经做到了,但我真的很想得到一些帮助来克服这最后的障碍。非常感谢
解决方案
缺少级联关系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>
在数据库中,我插入了一些数据,例如Part
和PartSupplier
。
结果。
推荐阅读
- python - 从熊猫日期范围功能中删除时间
- scala - Scala Char 自动转换为 Integer
- python - 第 51 行:TypeError:列表索引必须是整数,而不是浮点数有什么问题请帮助我
- c# - 使用来自 c# 控制台的参数打印 SSRS .rdl 报告
- javascript - Bootstrap 5 选项卡窗格无法正确使用 flexbox
- javascript - 我不断收到此类型错误:无法读取未定义的属性“地图”
- angular - 使用管道角11从字符串中删除特定字符或单词
- php - 根据子值合并多维数组的子数组
- java - Java Spring如何检查两个对象是否相等并且其中一个是LAZY加载的
- logging - 机器人框架日志关键字 - 参数“控制台”不起作用