Sti*_*ian 3 linq entity-framework-core ef-core-2.0
我正在尝试通过以下查询加载产品标识符Label的列表(即产品标识符的名称,例如“ EAN”,“产品编号”等)ProductIdentifiers:
ICollection<ProductIdentifierInType> typeIds =
_context.ProductIdentifiersInTypes
.Include(t => t.Identifier.Label)
.OrderBy(o => o.SortOrder).ToList();
Run Code Online (Sandbox Code Playgroud)
VS给了我智慧t.Identifier.Label。解决方案编译良好。这是我得到的运行时错误:
InvalidOperationException:属性“ Label”不是实体类型“ ProductIdentifier”的导航属性。“ include(string)”方法只能与“。”一起使用。导航属性名称的分隔列表。
以下是相关的模型类:
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; }
}
Run Code Online (Sandbox Code Playgroud)
我的DbContext:
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 ...
}
Run Code Online (Sandbox Code Playgroud)
我曾试图继承IdentityDbContext像解决这个问题,但没有任何区别。
怎么了?
如注释中所指出,“包含”仅适用于“导航”属性。
而不是.Include(t => t.Identifier.Label)只尝试做.Include(t => t.Identifier)
然后使用列表访问Label属性。