I am creating a restaurant app, I have the entity Dish
and the entity Ingredients
, both of which should be related so that I can know what ingredients I have in each dish, my idea was to make a joined entity with both DishId
and IngredientId
, and that table will handle every dish - ingredient combination.
Here is what the entity looks like:
public class DishIngredient : AuditableBaseEntity
{
public int DishId { get; set; }
public Dish Dish { get; set; }
public int IngredientId { get; set; }
public Ingredient Ingredient { get; set; }
}
I am trying to create a method in my controller that sends me every Dish and the corresponding ingredients of the dish using a navigation property List<Ingredient> Ingredients { get; set; }
to my DishViewModel
but I'm not really sure if that's the correct implementation.
public class DishViewModel
{
public int Id { get; set; }
public string Name { get; set; }
public double Price { get; set; }
public int People { get; set; }
public string Category { get; set; }
public List<Ingredients> Ingredients { get; set; }
}
My problem is that I am not really sure about how to get the dishes with the ingredients together.
I have tried to use the Include method in EF Core to get the DishIngredients
table so that then I can get the Ingredients
table but that does not seem to be sending any data (I think that the lack of any filtering is causing the DishIngredients
table to be sent null)
public async Task<List<Dish>> GetDishesWithIngredients()
{
var result = _dbContext.Dishes
.Include(di => di.DishIngredients)
.ToList();
return result;
}
My problem is that I am not really sure about how to get the dishes with the ingredients together.
You may follow this document:
Add nevigation property in Dish entity and Ingredient entity:
public class Dish
{
public int Id { get; set; }
public string? DishProp { get; set; }
.....
public List<Ingredient> Ingredients { get; set; } = default!;
public List<DishIngredient> DishIngredients { get; set; } = default!;
}
public class Ingredient
{
public int Id { get; set; }
public string? IngredientProp{ get; set; }
.....
public List<Dish> Dishes { get; set; } = default!;
public List<DishIngredient> DishIngredients { get; set; } = default!;
}
public class DishIngredient
{
public int DishId { get; set; }
public Dish Dish { get; set; } = default!;
public int IngredientId { get; set; }
public Ingredient Ingredient { get; set; } = default!;
}
configure as below in dbcontext:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Dish>().HasMany(x => x.Ingredients).WithMany(y => y.Dishes).UsingEntity<DishIngredient>();
}
Read Dishs with Ingredients:
var result = await _context.Dish.Include(x => x.Ingredients).ToListAsync();