Adriano Silva
Adriano Silva

Reputation: 51

EFCore Include without navigation property

How can I do an Include without navigation property?
I can use the navigation property to get the subcategories, but after that I need to get all the ingredients for those subcategories (and I don't have the nav. property for the ingredients because of the Aggregate root boundaries). And I don't know how I can get them.

Database Code

var cat = _context.Categories.Include(s => s.SubCategories).ToList();

Upvotes: 2

Views: 3257

Answers (1)

Louis Ingenthron
Louis Ingenthron

Reputation: 1368

As far as I know, you can't, not with Include. For that, you'd need to use a separate request/query using the linked key as a clause.

But if you need to reference it in this way, why not make it a navigation property? That's exactly what they're for.

So, if you have a category ID, then this is how you'd loop through the ingredients. You should be able to work from this to get where you need to go:

var catId = 999;
foreach(var subCat in _context.SubCategories.Where(u => u.CategoryId == catId))
{
    foreach(var ingredient in _context.Ingredients.Where(u => u.SubCategoryId == subCat.Id))
    {
        // do work on 'ingredient'
    }
}

Upvotes: 2

Related Questions