Relationship between entities (EF basics article 7), entity ef

Source: Internet
Author: User

Relationship between entities (EF basics article 7), entity ef

The relationship between EF entities is divided:

1. One-to-one;

2. one-to-many;

3. Many to many;

 

One-to-one relationship:

Between Student and StudentAddress:

public partial class Student    {        public int StudentID { get; set; }        public string StudentName { get; set; }        public Nullable<int> StandardID { get; set; }        public string RowVersion { get; set; }            public virtual Standard Standard { get; set; }        public virtual StudentAddress StudentAddress { get; set; }    }
 public partial class StudentAddress    {        public int StudentID { get; set; }        public string Address1 { get; set; }        public string Address2 { get; set; }        public string City { get; set; }        public string State { get; set; }            public virtual Student Student { get; set; }    }

One-to-multiple relationship:

There are many of the above. Here is an example: Teacher and Standard.

 public partial class Standard    {        public Standard()        {            this.Students = new HashSet<Student>();            this.Teachers = new HashSet<Teacher>();        }            public int StandardID { get; set; }        public string StandardName { get; set; }        public string Description { get; set; }            public virtual ICollection<Student> Students { get; set; }        public virtual ICollection<Teacher> Teachers { get; set; }    }
 public partial class Teacher    {        public Teacher()        {            this.Courses = new HashSet<Course>();        }            public int TeacherID { get; set; }        public string TeacherName { get; set; }        public Nullable<int> StandardID { get; set; }        public string TeacherType { get; set; }            public virtual ICollection<Course> Courses { get; set; }        public virtual Standard Standard { get; set; }    }

Many-to-many relationship:

Between Student and Course:

 public partial class Student    {        public Student()        {            this.Course = new HashSet<Course>();        }            public int StudentID { get; set; }        public string StudentName { get; set; }        public Nullable<int> StandardID { get; set; }        public string RowVersion { get; set; }            public virtual Standard Standard { get; set; }        public virtual StudentAddress StudentAddress { get; set; }        public virtual ICollection<Course> Course { get; set; }    }
 public partial class Course    {        public Course()        {            this.Student = new HashSet<Student>();        }            public int CourseID { get; set; }        public string CourseName { get; set; }        public string Location { get; set; }        public Nullable<int> TeacherID { get; set; }            public virtual Teacher Teacher { get; set; }        public virtual ICollection<Student> Student { get; set; }    }

Note: EF supports many-to-many relationships. It is only a join table (StudentCourse here) that does not contain tables of columns other than Student and Course primary keys. If the associated table contains other columns, such as the date of deletion (Datedelete), you must perform the operation manually to implement multi-to-many relationships;

We use the XML view to open the object data model. In SSDL, we can see the StudentCourse EntitySet entity set. CSDL does not include the StudentCourse entity set, it is mapped to the navigation properties of Student and Course, and it can be seen in MSL (C-S Mapping) in the <AssociationSetMapping> node

 

So, the many-to-many relationship is in the C-S ing section of the Entity Data Model, so when you add a Course to the Student table or add a Student to the Course table, when you save the data, the primary keys of the students or courses you inserted will be added to the StudentCourse table. Therefore, this ing not only facilitates the association between the two entities, in addition, it manages addition, deletion, query, and modification operations;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.