【发布时间】:2021-05-21 03:43:41
【问题描述】:
我有一个产品表和一个类别表。所有产品都有一个 FK-categoryId。 如何停止多次加载数据?像这样:
类:
public partial class Product
{
public Product()
{
OrderItem = new HashSet<OrderItem>();
ProductPictureMapping = new HashSet<ProductPictureMapping>();
}
public int Id { get; set; }
public string Name { get; set; }
public int CategoryId { get; set; }
public virtual Category Category { get; set; }
}
public partial class Category
{
public Category()
{
Product = new HashSet<Product>();
}
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Product> Product { get; set; }
}
控制器方法:
var allProduct = _context.Product
.Include("Category")
.ToListAsync();
输出:
它返回一个产品,然后是一个类别,然后是 Category 下的所有产品 请注意,我已经在 startup.csNewtonsoft.Json.ReferenceLoopHandling.Ignore 中禁用了循环;
[{
"id": 1,
"name": "Redmi Note 7",
"category": {
"id": 2,
"name": "Mobile",
"product": [
{
"id": 2,
"name": "Mi Note 9",
"category":null
}]
},
{
"id": 2,
"name": "Mi Note 9",
"category": {
"id": 1,
"name": "Redmi Note 7",
"category":null
}
}]
【问题讨论】:
标签: c# .net linq .net-core entity-framework-core