Entity relationship EF in

1. one relationship type

Student and StudentAddress figure above is one to one (zero) relationship, a student can have only one or no addresses.

Entity Framework Student entity navigation property added to StudentAddress entity, the StudentAddress entity navigation property added to the Student entity.

StudentAddress class StudentID both the PrimaryKey (primary key), is a ForeignKey Student class (foreign key).

public partial class Student
{
    public Student() { this.Courses = new HashSet<Course>(); } public int StudentID { get; set; } public string StudentName { get; set; } public Nullable<int> StandardId { get; set; } public byte[] RowVersion { get; set; } //实体导航属性 public virtual StudentAddress StudentAddress { get; set; } } public partial classStudentAddress {   // the same time is the primary key and the foreign key public int StudentID { GET; SET ;} public String an Address1 { GET; SET ;} public String Address2 { GET; SET ;} public String City { GET; SET ;} public String State { GET; SET ;} // entity navigation property public Virtual Student Student { GET; SET ;}}

2. to-many relationship type

Teacher and Course-many relationship. (A teacher may teach many courses, a course taught by only one teacher)

Standard relationship with Teacher entity with one to many. (A rating level may be assigned to more than a teacher, a teacher can have only one score)

public partial class Standard
{
    public Standard() { this.Teachers = new HashSet<Teacher>(); } public int StandardId { get; set; } public string StandardName { get; set; } public string Description { 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> TeacherType { get; set; } //外键 public Nullable<int> StandardId { get; set; } //实体导航属性 public virtual Standard Standard { get; set; } }

Standard entity having a set of attributes navigation Teachers (Please note that it is plural), Teacher in StandardId Standard is a foreign key.

3. Type-many relationship

Student Course and have many relationships. This means that a student can take many courses, and a course can also be taught to many students.

public partial class Student
{
    public Student() { this.Courses = new HashSet<Course>(); } public int StudentID { get; set; } public string StudentName { get; set; } public Nullable<int> StandardId { get; set; } public byte[] RowVersion { get; set; } //集合导航属性 public virtual ICollection<Course> Courses { get; set; } } public partial class Course { public Course() { this.Students = new HashSet<Student>(); } public int CourseId { get; set; } public string CourseName { get; set; } //集合导航属性 public virtual ICollection<Student> Students { get; set; } }

Both classes have a set of navigation attributes.

Note: Entity Framework only the master key table when two (and only StudentId CourseId) automatically maintain many relationships among tables. When we add to a Student Course Course or to add a Student, execution SaveChange (), EF will automatically be inserted into the corresponding intermediate table StudentId and CourseId. If the middle of the table contains other columns, EDM entities will create an intermediate table, EF no longer automatically maintain the middle of the table, then we need to manually manage the CRUD operations-many entities.

Guess you like

Origin www.cnblogs.com/1016391912pm/p/12032900.html