gpt4 book ai didi

linq - 属性 'a' 不是实体类型 'b' 的导航属性。为什么不?

转载 作者:行者123 更新时间:2023-12-01 09:16:43 26 4
gpt4 key购买 nike

我正在尝试加载产品标识符列表 Label s(即产品标识符的名称,例如“EAN”、“产品编号”等)来自 ProductIdentifiers使用此查询:

ICollection<ProductIdentifierInType> typeIds =
_context.ProductIdentifiersInTypes
.Include(t => t.Identifier.Label)
.OrderBy(o => o.SortOrder).ToList();

VS 为我提供了 t.Identifier.Label 的智能感知.该解决方案编译得很好。这是我得到的运行时错误:

InvalidOperationException: The property 'Label' is not a navigation property of entity type 'ProductIdentifier'. The 'Include(string)' method can only be used with a '.' separated list of navigation property names.



以下是相关的模型类:
public class ProductIdentifierInType
{
public int Id { get; set; }
public int ProductTypeId { get; set; }
public int SortOrder { get; set; }

public ProductIdentifier Identifier { get; set; }
public ProductType Type { get; set; }
}

public class ProductIdentifier
{
public int Id { get; set; }
public string Label { get; set; }
public int SortOrder { get; set; }

public ICollection<ProductIdentifierInType> TypeIdentifiers { get; set; }
}

我的数据库上下文:
public class MyStoreContext : DbContext // IdentityDbContext
{
public MyStoreContext(DbContextOptions options) : base(options) { }

// some unrelated tables ...

public DbSet<ProductIdentifierInType> ProductIdentifiersInTypes { get; set; }
public DbSet<ProductIdentifier> ProductIdentifiers { get; set; }

// some more, unrelated tables ...
}

我试过从 IdentityDbContext 继承就像 this question 中的解决方案,但这没有任何区别。

怎么了?

最佳答案

正如评论中指出的, Include 仅适用于 Navigation 属性。

而不是做 .Include(t => t.Identifier.Label)尝试只做 .Include(t => t.Identifier)
然后使用列表访问标签属性。

关于linq - 属性 'a' 不是实体类型 'b' 的导航属性。为什么不?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48903579/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com