I have a Cake table and an ingredient table a cake is made of many ingredients and an ingredient can be in many cakes so I've created a many to many mapping with join table cake_ingredients
. In addition however different quantities of each ingredient is used for each cake. For example in cake flavor A you could require 1.25kg of flour while in cake flavor B you require 1.5kg. Due to the different quantities of ingredients the cost for each ingredient used in each cake will be different. Therefore my join table needs to be with payload, the additional columns are quantity and cost. I've created this relationship successfully by following this tutorial.
I can add cake_id
and ingredient_id
successfully to the join table by modifying the following block of code that I got from the above tutorial
private void UpdateInstructorCourses(string[] selectedCourses, Instructor instructorToUpdate)
{
if (selectedCourses == null)
{
instructorToUpdate.Courses = new List<Course>();
return;
}
var selectedCoursesHS = new HashSet<string>(selectedCourses);
var instructorCourses = new HashSet<int>
(instructorToUpdate.Courses.Select(c => c.CourseID));
foreach (var course in db.Courses)
{
if (selectedCoursesHS.Contains(course.CourseID.ToString()))
{
if (!instructorCourses.Contains(course.CourseID))
{
instructorToUpdate.Courses.Add(course);
}
}
else
{
if (instructorCourses.Contains(course.CourseID))
{
instructorToUpdate.Courses.Remove(course);
}
}
}
Trouble comes in inserting quantity and cost (this will be calculated).
How do I go about this? I figure if someone could help me insert quantity I'll be able to insert the calculated cost.