Click here to Skip to main content
15,881,803 members
Articles / Web Development / ASP.NET
Tip/Trick

Configure One To Many Relationship in MVC and EntityFrameWork Code First, Code Migration Approach

Rate me:
Please Sign up or sign in to vote.
4.67/5 (4 votes)
20 Sep 2015CPOL2 min read 42K   10   3
Configure One To One and One To Many Relationship in MVC and EntityFrameWork Code first, code migration approach

Learn and build web application using MVC and Entity framework code first, code migration approach

Video: https://www.youtube.com/watch?v=GanjklfebEc&index=1&list=PLdmgFSerzv2Kl00tz0qY-jxDPnPowpRaT

Learn MVC and Entity framework code first, code migration approach using sample demo application

Video: https://www.youtube.com/watch?v=OhJtxRvgeK0&list=PLdmgFSerzv2Lhj5dFrD75sz26qGdxpo8Z


Overview

Relationships scenario(One To Many Relationship)

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

    Image 1

  • Course model contains the following properties defined.

    Image 2

  • Open the student model and add the Navigational property for course.
  • Configure the Foreign key by adding CourseId (Primary key of the Course class model) entity in the Student class.
  • 1 to 1 relationship (1 Student can select 1 Course) and
  • 1 to Many relationship ( 1 Course can have Many Students) is established.

    Image 3

  • Generate migration file by executing the command "ADD-Migration" in the package manager console for configuring Foreign Key in Student table in the database side.
  • Script gets generated in migration file.

    Image 4

  • Once the script got generated, update the database.

    Image 5

    Note: Establishing foreign key relationship to the model with student table containing data will generate the foreign key constraint error since Course Id is defined not nullable.

    Image 6

  • 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 established to the Course table.

    Image 7

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

    Image 8

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

    Image 9

  • Modify the Edit Action method and Edit.cshtml viewpage:

    Image 10

  • Add the Dropdownlist to display and provide options to choose the course in the dropdown.

    Image 11

  • 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.

Image 12

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer HP
India India
I am working in HP as a dot net developer.Working in .net technologies(MVC,Entityframework,LINQ,Jquey

Comments and Discussions

 
GeneralMy vote of 5 Pin
PVX00724-Sep-15 7:32
PVX00724-Sep-15 7:32 
Ingenious Dileep!!! Simple to follow but brilliant! Thank you for posting this article.
GeneralRe: My vote of 5 Pin
Dileep Ashok26-Sep-15 19:57
professionalDileep Ashok26-Sep-15 19:57 
GeneralRe: My vote of 5 Pin
log9810-Sep-17 2:12
log9810-Sep-17 2:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.