locked
Many to Many Relationship in MVC RRS feed

  • Question

  • User-2143443722 posted

    I am a beginner in ASP.NET MVC. I have understood all the basic concepts of ASP.NET MVC (how it works?). I am stuck at Relationships especially Many to many.. 
    i got two tables STUDENTS and COURSES. Many to many relationship between these two tables. 
    My question is:
    How i will tell which student is enrolled in which course from the VIEW of Create Student method?
    Seen many tutorials but they give input of student and their relative courses in the Code, How can i do it in create student View? 

    THANKS in Advance if anyone could help me out!

    Monday, April 6, 2015 8:39 AM

Answers

  • User-474980206 posted

    in relational databases if you have two tables with a many to many relationship, you need a third "join" table. in your case this table would have a student id, a course id and a unique key (usually auto). EF does not support additional data in this join table. once you have this table, you can add navigation from students to courses, and courses to students.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 6, 2015 11:23 AM
  • User1644755831 posted

    Hello zubairmohsin,

    As bruce (sqlwork.com) suggested you need another join table like coursestudent which will hold mapping between CourseId and StudentId.

    Here is an example that displays exactly what you want.

    http://www.entityframeworktutorial.net/code-first/configure-many-to-many-relationship-in-code-first.aspx

    Hope this helps.

    With Regards,

    Krunal Parekh

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 8, 2015 2:26 AM

All replies