The property 'a' is not a navigation property of entity type 'b'. Why not?

ef-core-2.0 entity-framework-core linq

Question

I'm trying to load a list of product identifier Labels (i.e. names of product identifiers, such as "EAN", "Product number", etc.) from ProductIdentifiers with this query:

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

VS gives me intellisense for t.Identifier.Label. The solution compiles fine. This is the runtime error I get:

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.

Here are the relevant model classes:

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; }
}

My 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 ...
}

I have tried inheriting from IdentityDbContext like the solution in this question, but that didn't make any difference.

What is wrong?

1
4
2/21/2018 11:17:27 AM

Accepted Answer

As pointed out in the comments the Include is for the Navigation property only.

Instead of doing .Include(t => t.Identifier.Label) try to do only .Include(t => t.Identifier)

Then access the Label property using the List.

6
2/21/2018 12:36:19 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow