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
![](https://4.bp.blogspot.com/-n_0nk-22ZmQ/VMORB5E-nSI/AAAAAAAAA1A/CGcCOZTpqLA/s1600/p3.PNG)
- 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
![](https://2.bp.blogspot.com/-HyJfwaRYOvY/VMOTmB99arI/AAAAAAAAA1I/BgGZYFWiYuc/s1600/p1.PNG)
- Execute the command "Add-Migration" in package manager console and update the database for adding the trainer table in the SQL Server database
![](https://1.bp.blogspot.com/-YR2nimtA-80/VMOULnmibEI/AAAAAAAAA1Q/2ng0CtWdJNI/s1600/p2.PNG)
- Build the solution and create a new model for "
TrainsOnCourse
" for storing the collections selected in the list box.
![](https://1.bp.blogspot.com/-eYtouErPiPc/VMOVmUuHxiI/AAAAAAAAA1g/vbqC2CZaIfM/s1600/p4.PNG)
- Add a
DBSet
entity property for the TrainsOnCourse
in the DBContext
class.
![](https://4.bp.blogspot.com/-qD9af-ugh4E/VMOXyCkn2DI/AAAAAAAAA1o/K3TGjcMb0Sg/s1600/p6.PNG)
- Modify the Trainer model for adding navigational property and for saving the collection items selected in the
listbox
.
![](https://4.bp.blogspot.com/-L6ttLcdT2Mc/VMObPMVXRQI/AAAAAAAAA2A/tG7Bgn1SUSQ/s1600/p9.PNG)
- 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.
![](https://3.bp.blogspot.com/-AqWQTg7uB7I/VMOZ8g4mdGI/AAAAAAAAA1w/e5K7PKJtc3A/s1600/p7.PNG)
- Open the SQL Server and new table "
TrainsOnCourse
" table should be created with 2 foreign keys.
![](https://1.bp.blogspot.com/-BI8FwsdEtbM/VMOaVYDxodI/AAAAAAAAA14/ZhrgXc2-plY/s1600/p8.PNG)
- Add a new Controller "
Trainer
". - Select the "Tranier.cs" model and "
DNSCContext
".
![](https://3.bp.blogspot.com/-UyxrZTxA1LU/VMOcLQue-tI/AAAAAAAAA2I/9HClwpBgSJE/s1600/p10.PNG)
- Modify the Create Action method to add multiselect list values to be populated from the
Course
table from the database.
![](https://3.bp.blogspot.com/-QQbg2i7QSXg/VMOdBfOvytI/AAAAAAAAA2Q/wdbalZS3Qzs/s1600/p11.PNG)
- 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
![](https://4.bp.blogspot.com/-Qc678JwgjiM/VMOjMvzTIpI/AAAAAAAAA20/GoyfUYk3Bzw/s1600/p13.PNG)
- Save the collection list items to the database.
- Modify the
HTTPPost
create action method to save the selected items.
![](https://4.bp.blogspot.com/-x20e8eor2ZY/VMOxN3rWLRI/AAAAAAAAA3E/rfKd7gmf2VU/s1600/p15.PNG)
- Modify the Edit Action method in the Trainer controller for handling
Listbox
control.
![](https://1.bp.blogspot.com/-toA_6hyXcGA/VMOy_ksYb7I/AAAAAAAAA3Q/4MCjSHiUSvg/s1600/p16.PNG)
- Open the "Edit.cshtml" view page of trainer and add the
ListBox
control.
![](https://2.bp.blogspot.com/-Ub8M6p6r2CI/VMOzzaFwyRI/AAAAAAAAA3Y/uL1W13dE0H0/s1600/p17.PNG)
- Open the
HttpPost
Edit Action method of the Trainer controller and perform the required operations.
![](https://2.bp.blogspot.com/-QdxnquWuivs/VMO0zm4JrTI/AAAAAAAAA3k/yayCssH8mzo/s1600/p18.PNG)
- Test the application with
listbox
values selected with create and edit operations.
I am working in HP as a dot net developer.Working in .net technologies(MVC,Entityframework,LINQ,Jquey