【发布时间】:2021-09-08 08:06:06
【问题描述】:
我有两个表 - Products 和 ProductRelations。他们是这样的:
public class Product
{
public int Id { get; set; }
public string SKU { get; set; }
public string Name { get; set; }
public double Price { get; set; }
public IList<ProductRelation> ProductRelations { get; set; }
}
public class ProductRelation
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public int ProductId { get; set; }
public int RelatedProductId { get; set; }
//[ForeignKey("ProductId")]
public Product Product { get; set; }
//[ForeignKey("RelatedProductId")]
public Product RelatedProduct { get; set; }
}
我收到错误InvalidOperationException: Unable to determine the relationship represented by navigation 'Product.ProductRelations' of type 'IList<ProductRelation>'. Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.
我从未使用过 OnModelCreating。遵循命名约定通常就足够了。
如果我从 ProductRelation 类中删除 public Product RelatedProduct { get; set; },错误就会消失。
我错过了什么?
【问题讨论】:
标签: c# asp.net-core ef-core-5.0