'Fetch Products from database with category and subcategory attached
I am building a web api and want to fetch all the products from product table I have in my database and I also want to include the category and subcategory which are in two other tables. How do I do then? So one product should display this Category = Electronics Subcategory = TV and product = LG TV 40
Solution 1:[1]
If you are using Entity Framework Core and you want to load a foreign key navigational property, then load another navigational property that is inside it:
var products = db.Products
.Include(x => x.Category)
.ThenInclude(x => x.Subcategory)
.ToList();
// Use .Include() to load a navigational property
// Use .ThenInclude() to load a navigational property's navigational property
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |