Entity Framework Tutorial
Lesson 32 of 69 · Part 25 Entity for BridgeTable in many to many relationship code first · Database
Part 25 Entity for BridgeTable in many to many relationship code first
Text version of the video
http://csharp-video-tutorials.blogspot.com/2014/06/part-25-entity-for-bridgetable-in-many_12.html
Slides
http://csharp-video-tutorials.blogspot.com/2014/06/part-25-entity-for-bridgetable-in-many.html
Entity Framework – All Text Articles
http://csharp-video-tutorials.blogspot.com/2014/05/entity-framework-tutorial.html
Entity Framework – All Slides
http://csharp-video-tutorials.blogspot.com/2014/05/entity-framework-tutorial-slides.html
Entity Framework Playlist
Dot Net, SQL, Angular, JavaScript, jQuery and Bootstrap complete courses
https://www.youtube.com/user/kudvenkat/playlists?view=1&sort=dd
In this video we will discuss creating an Entity for the bridge table in a many-to-many relationship with code first. This is continuation to Part 24. Please watch Part 24 before proceeding.
We want the entity framework to create the following tables.
1. Courses – CourseID should be the Primary Key
2. Students – StudentID should be the Primary Key
3. StudentCourses – Composite primary key consisiting of CourseID & StudentID columns. CourseID should also be the foreign key referencing CourseID column in Courses table. StudentID should also be the foreign key referencing StudentID column in Students table.
To achieve this we would design Student, Course & StudentCourse classes as shown below.
public class Course
{
public int CourseID { get; set; }
public string CourseName { get; set; }
public IList[StudentCourse] StudentCourses { get; set; }
}
public class Student
{
public int StudentID { get; set; }
public string StudentName { get; set; }
public IList[StudentCourse] StudentCourses { get; set; }
}
public class StudentCourse
{
public Course Course { get; set; }
public Student Student { get; set; }
[Key, Column(Order = 1)]
public int StudentID { get; set; }
[Key, Column(Order = 2)]
public int CourseID { get; set; }
public DateTime EnrolledDate { get; set; }
}