I am struggling to create the necessary attributes and code to allow me to retrieve all information from 3 tables I have.
The tables are:
Recipe
table:
Column | Type |
---|---|
RecipeId | int (Key) |
Title | varchar |
Ingredients
table:
Column | Type |
---|---|
IngredientId | int (Key) |
Description | varchar |
Ingredients_Mapping
table:
Column | Type |
---|---|
RecipeId | int (Key) |
IngredientId | int (Key) |
Quantity | int (Key) |
Hopefully the above makes sense. Each recipe may contain many ingredients. When I've pulled back details before it has been a simple one and I've added a .Include(x => x.Whatever)
to extract the data from the joining table.
Here's the code:
public class Recipe
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[Required]
public string Title { get; set; }
[NotMapped]
public Ingredient[] Ingredients { get; set; }
}
public class Ingredient
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[Required]
public string Title { get; set; }
}
public class IngredientMapping
{
[Key]
[Required]
public int RecipeId { get; set; }
[Key]
[Required]
public int IngredientId { get; set; }
[Required]
public int Quantity { get; set; }
}
public async Task<List<Recipe>> GetAllRecipesAsync()
{
return await _MyDbContext.Recipes
.Include(x => x.???)
.OrderBy(b => b.Title).ToListAsync();
}
Could somebody please advise how I can do this please?