Saturday 24 January 2015

Configure Many-To-Many Relationship and ListBox control in MVC and Entty Frame Work

Relationship scenario (Many to Many Relationship)


  • Trainer can train on multiple Courses 
  • Eg- T1 Trainer trains on C1 and C2 Courses

  • Course can be handled by multiple Trainers
  • Eg- 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 manger 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


Configure One To Many Relationship in MVC and EntityFrameWork



Relationships scenario( One To Many Relationship)

https://www.youtube.com/watch?v=9qyRIW0FTZ8
  • Course can have multiple Students
  • Student can select only one course




















  • Course Table model
























  • Open the student table and add the Navigational property for course table
  • Configure the Foreign key as CourseId to the Student Table


























  • Execute the command  "ADD-Migration" in the package manager console after the modification in model file for configuring Foreign Key in Student table 
  • Script gets generated 










  • Once the script got generated, update the database.
















































  • Once after executing the "Update-database" command in package manger console, script present in the "up method " of the configuration file will be executed in the SQL Server and Foreign Key will be created 





  • Add the required changes in the create action method in the Student Controller













  • Create a dropdownlist in the viewpage for selecting the course for particular student in the "create.cshtml" view page
















  • Modify the Edit Action method and Edit.cshtml viewpage 


























  • Run the application and test it. 
  • DropdownList for selecting the course will be present and 
  • 1 to Many Relationship and ForeignKey is configured and established