4

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?

Stian
  • 1,522
  • 2
  • 22
  • 52
  • *The property 'Label' is not a navigation property* tells exactly what's wrong. – Gert Arnold Feb 21 '18 at 10:32
  • The only navigation property you have on ProductIdentifierInType is Type. Include only works on Navigation properties. Is there some code missing as well? What is t.identifier ? – illug Feb 21 '18 at 10:59
  • @illug Oh, my bad! I accidently deleted some code when pasting into this question. I have updated it now. `Identifier` is the navigation link from `ProductIdentifierInType` to `ProductIdentifier`. `Label` is found within `ProductIdentifier`. – Stian Feb 21 '18 at 11:20
  • @GertArnold I accidently deleted some rather important code before: `public ProductIdentifier Identifier { get; set; }` It's there now. It's the navigation link from `ProductIdentifierInType` to `ProductIdentifier`. Isn't that properly linked? `Label` is found in `ProductIdentifier`. – Stian Feb 21 '18 at 11:23
  • I assumed that. It's simple: 'Label' is not a navigation property. – Gert Arnold Feb 21 '18 at 11:26
  • @GertArnold I don't get it. `ProductIdentifierInType` has `Identifier` as navigation. Under `Identifier`, we find `Id`, `Label` and `SortOrder`. Why isn't `Label` a navigation property? – Stian Feb 21 '18 at 11:59
  • https://stackoverflow.com/q/1262307/861716 – Gert Arnold Feb 21 '18 at 12:27

1 Answers1

6

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.

illug
  • 793
  • 1
  • 9
  • 23