Relationship Scenario (Many to Many Relationship)
Video: https://www.youtube.com/watch?v=nzmk1hrYYtQ
Relationship scenario (Many to Many Relationship)
- Trainer can train on multiple Courses
- E.g.- T1 Trainer trains on C1 and C2 Courses
- Course can be handled by multiple Trainers
- E.g.- C3 Course is handled by T3, T4 and T5trainers

- Many to Many Relationship requires a new table (
TrainsOnCourse
) for storing the collections that are selected in the list box - Create a new Model Trainer

- Execute the command "Add-Migration" in package manager console and update the database for adding the trainer table in the SQL Server database

- Build the solution and create a new model for "
TrainsOnCourse
" for storing the collections selected in the list box.

- Add a
DBSet
entity property for the TrainsOnCourse
in the DBContext
class.

- Modify the Trainer model for adding navigational property and for saving the collection items selected in the
listbox
.

- Build the solution and execute the "Add-Migration" command in the package manager console for adding the new table "
TrainsOnCourse
". - Update the database for running the migration file in the SQL Server by executing the "update-database" command in package manager console.

- Open the SQL Server and new table "
TrainsOnCourse
" table should be created with 2 foreign keys.

- Add a new Controller "
Trainer
". - Select the "Tranier.cs" model and "
DNSCContext
".

- Modify the Create Action method to add multiselect list values to be populated from the
Course
table from the database.

- Add a
ListBoxControl
in "Create.cshtml" view page in Trainer folder.
- Run the application and goto "Trainer/Create" Actionmethod by entering in the url.
- We should be able to see the ListBox with set of values

- Save the collection list items to the database.
- Modify the
HTTPPost
create action method to save the selected items.

- Modify the Edit Action method in the Trainer controller for handling
Listbox
control.

- Open the "Edit.cshtml" view page of trainer and add the
ListBox
control.

- Open the
HttpPost
Edit Action method of the Trainer controller and perform the required operations.

- Test the application with
listbox
values selected with create and edit operations.