The relationships between EF entities are divided into:
1. one to one;
2. A pair of more;
3. Many-to-many;
One-to-one relationship:
Between student and Studentaddress:
Public Partial classStudent { Public intStudentID {Get;Set; } Public stringStudentname {Get;Set; } Publicnullable<int> Standardid {Get;Set; } Public stringRowVersion {Get;Set; } Public VirtualStandard Standard {Get;Set; } Public VirtualStudentaddress studentaddress {Get;Set; } }
Public Partial classstudentaddress { Public intStudentID {Get;Set; } Public stringAddress1 {Get;Set; } Public stringAddress2 {Get;Set; } Public stringCity {Get;Set; } Public stringState {Get;Set; } Public VirtualStudent Student {Get;Set; } }
One-to-many relationships:
Many of them, just cite one example, teacher and standard
Public Partial classStandard { PublicStandard () { This. Students =NewHashset<student>(); This. Teachers =NewHashset<teacher>(); } Public intStandardid {Get;Set; } Public stringStandardName {Get;Set; } Public stringDescription {Get;Set; } Public VirtualIcollection<student> Students {Get;Set; } Public VirtualIcollection<teacher> Teachers {Get;Set; } }
Public Partial classTeacher { PublicTeacher () { This. Courses =NewHashset<course>(); } Public intTeacherid {Get;Set; } Public stringTeacherName {Get;Set; } Publicnullable<int> Standardid {Get;Set; } Public stringTeachertype {Get;Set; } Public VirtualIcollection<course> Courses {Get;Set; } Public VirtualStandard Standard {Get;Set; } }
Many-to-many relationships:
Between student and course:
Public Partial classStudent { PublicStudent () { This. Course =NewHashset<course>(); } Public intStudentID {Get;Set; } Public stringStudentname {Get;Set; } Publicnullable<int> Standardid {Get;Set; } Public stringRowVersion {Get;Set; } Public VirtualStandard Standard {Get;Set; } Public VirtualStudentaddress studentaddress {Get;Set; } Public VirtualIcollection<course> Course {Get;Set; } }
Public Partial classCourse { PublicCourse () { This. Student =NewHashset<student>(); } Public intCourseID {Get;Set; } Public stringCoursename {Get;Set; } Public stringLocation {Get;Set; } Publicnullable<int> Teacherid {Get;Set; } Public VirtualTeacher Teacher {Get;Set; } Public VirtualIcollection<student> Student {Get;Set; } }
Note: It is important to note that EF supports many-to-many relationships, only the associated table (here is Studentcourse), and does not contain tables for any other columns except the student and course primary keys. If the associated table contains other columns, such as the delete date (datedelete), then you have to manually go to the operation to achieve a many-to-many relationship;
We open the Entity Data Model as an XML view, and we can see that in SSDL, you can see the Studentcourse entityset entity set, and the CSDL does not contain the Studentcourse entity set, instead of Yes, It is mapped to the navigation properties of student and course and can be seen in the MSL (C-s Mapping) in the <AssociationSetMapping> node
So, many-to-many relationships are in the C-s Mapping section of the Entity Data model, so when you add a course to the student table, or add a course to the student table, and then you save it, You will be inserted into the student's or curriculum's primary key to the Studentcourse table, so this mapping is not only convenient to correlate the two entities, but also the management of additions and deletions to change the operation;
Relationships between Entities (EF Basic Series 7)