【问题标题】:Unable to determine the relationship represented by navigation无法确定导航所代表的关系
【发布时间】: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&lt;ProductRelation&gt;'. 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


    【解决方案1】:

    您需要Product 中的多个集合。试试这样的:

    public class Product
    {
        public int Id { get; set; }
        public string SKU { get; set; }
        public string Name { get; set; }
    
        [InverseProperty(nameof(ProductRelation.Product)]
        public IList<ProductRelation> ProductRelationsLeft { get; set; }
        [InverseProperty(nameof(ProductRelation.RelatedProduct)]
        public IList<ProductRelation> ProductRelationsRight { get; set; }
    }
    

    你也可以通过fluent api尝试下一个设置,提供默认的关系集合参数(collection):

    collection - 此关系另一端的集合导航属性的名称。如果为 null 或未指定,则关系的另一端没有导航属性。

    modelBuilder.Entity<ProductRelation>()
         .HasOne(pt => pt.Product)
         .WithMany() // leave empty
         .HasForeignKey(pt => pt.ProductId)
         .OnDelete(DeleteBehavior.Restrict); 
    
    modelBuilder.Entity<ProductRelation>()
        .HasOne(pt => pt.RelatedProduct)
        .WithMany(t => t.ProductRelations)
        .HasForeignKey(pt => pt.RelatedProductId); 
    

    【讨论】:

    • InverseProperty 一开始似乎修复了错误,但在查询Products 时出现循环错误。例如。 var products = await _dbContext.Products.Include(x =&gt; x.ProductRelationsRight).ThenInclude(x =&gt; x.RelatedProduct).OrderBy(x =&gt; x.Name).AsNoTracking().ToListAsync() 这是有道理的,但是有解决办法吗?还是我应该让逻辑不同?
    • @Mads 1) 你想用这个查询得到什么? 2) 请添加完整错误 3) 我会大胆猜测并说您实际上需要Include 而不是ThenInclude - 试试var products = await _dbContext.Products.Include(x =&gt; x.ProductRelationsRight).Include(x =&gt; x.RelatedProduct).OrderBy(x =&gt; x.Name).AsNoTracking().ToListAsync()
    • 我需要一个产品列表以及每个产品的相关产品和产品数据。例如,我可以询问具有相关产品的产品并获得相关产品价格的总和(不包括在 Product 类示例中)。
    • 另外,我不能.Include(x =&gt; x.RelatedProduct),因为它在产品类中不可用。
    • 完整错误InvalidOperationException: The Include path 'ProductRelationsRight-&gt;RelatedProduct' results in a cycle. Cycles are not allowed in no-tracking queries; either use a tracking query or remove the cycle.
    猜你喜欢
    • 1970-01-01
    • 2020-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-17
    • 2018-04-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多