简体   繁体   中英

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.

数据库 Code

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM