【发布时间】:2020-07-26 06:08:35
【问题描述】:
我有 2 张桌子。
产品和类别。
类别:
public string CategoryName { get; set; }
public ICollection<Product> Products { get; set; }
产品:
public string ProductName { get; set; }
[Required]
public string ProductDescription { get; set; }
[Required]
public int CategoryId { get; set; }
public Category Category { get; set; }
这是映射:
public class ProductMapping : IEntityTypeConfiguration<Product>
{
public void Configure(EntityTypeBuilder<Product> builder)
{
builder.HasOne(c => c.Category).WithMany(x => x.Products).HasForeignKey(x => x.CategoryId);
}
}
但是当我需要用CategoryName 返回产品的详细信息时,我会写这个查询:
var findProduct = await ProductEntity.Include(x => x.Category).Where(x => x.Id == key).FirstOrDefaultAsync();
但结果它显示了这个错误:
检测到不支持的可能对象循环。这可能是由于循环或对象深度大于允许的最大深度
错误是什么?我该如何解决这个问题???
【问题讨论】:
-
我想,你必须从
Product中删除Category -
@Sajid 为什么??????
-
我认为是问题的根源,如果你不删除它,寻找解决方案忽略它。 stackoverflow.com/questions/59199593/… 或必须是虚拟的。
-
这与 EF 无关 - EF 在循环引用方面绝对没有问题。
标签: c# asp.net entity-framework asp.net-core entity-framework-core