I have a situation which can be explained by below analogy.
Example: let's say we have 3 tables Categories => SubCategories => Products.
1 category can have many subcategories and 1 subcategory can have many products.
I am showing simple card for products details with category and subcategory names but for it I am writing the EF like.
var products = await _context.Products
.Where(x => x.IsDeleted == false)
.Include(x => x.SubCategory)
.Include(x => x.SubCategory.Category).ToListAsync()
The SQL generated is too expensive.
When the products are reached to the controller then Automapper starts it's magic to map according to my required view model.
I am new to Entity Framework Core and I have three questions:
- Is it there a better way to write above code?
- Can I return a view model directly from Entity Framework Core? In the above case, can I return a model with properties showing just names of Products, SubCategory and Category?
- If I can't return, then how can I convince myself to stop using Dapper?
ProjectToinstead. docs.automapper.org/en/latest/Queryable-Extensions.html