1

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.

cgitosh
  • 313
  • 3
  • 12
  • Please use paragraphs and capitals. Where is _the following block of code_? – Gert Arnold Feb 27 '12 at 15:06
  • The Instructor/Course example is not good for your purpose because you must map your relationships not as a single many-to-many relationship but instead as two one-to-many relationships. You must expose `cake_ingredients` as a real entity in your model; it's not just a technical link table but it carries "business meaning" (namely quantity and costs) => entity class required! An example how to model such a relationship (a "many-to-many with payload") is here: http://stackoverflow.com/a/7053393/270591 – Slauma Feb 27 '12 at 18:04
  • @GertArnold sorry the "following block of code" should have come before the code block above. Apologies for the mistake. – cgitosh Feb 28 '12 at 09:30
  • @Slauma Thank you, let me follow that example and get back. – cgitosh Feb 28 '12 at 09:31

0 Answers0