zoukankan      html  css  js  c++  java
  • Configure Many-to-Many relationship:

    Configure Many-to-Many relationship:

    Here, we will learn how to configure Many-to-Many relationship between the Student and Course entity classes. Student can join multiple courses and multiple students can join one course.

    Visit Entity Relationship section to understand how EF manages one-to-one, one-to-many and many-to-many relationships between the entities.

    Configure Many-to-Many relationship using DataAnnotation:

    Student class should have a collection navigation property for Course, and Course should have a collection navigation property for student, which will create a Many-to-Many relationship between student and course as shown below:

    public class Student
    {
        public Student() { }
    
        public int StudentId { get; set; }
        [Required]
        public string StudentName { get; set; }
    
        public int StdandardId { get; set; }
            
        public virtual ICollection<Course> Courses { get; set; }
    }
            
    public 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; }
    }
            

    The code shown above will create the following database, where Code-First will create a third joining table, CourseStudent, which will consist of the PK of both the tables, i.e. StudentId & CourseId:

    one-to-one relationship in code first

    Configure Many-to-Many relationship using Fluent API:

    You can use the Fluent API to configure a Many-to-Many relationship between Student and Course, as shown below:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
    
        modelBuilder.Entity<Student>()
                    .HasMany<Course>(s => s.Courses)
                    .WithMany(c => c.Students)
                    .Map(cs =>
                            {
                                cs.MapLeftKey("StudentRefId");
                                cs.MapRightKey("CourseRefId");
                                cs.ToTable("StudentCourse");
                            });
    
    }
            

    As you can see in the above example, .HasMany<Course>(s => s.Courses).WithMany(c => c.Students) says that Student and Course has many-to-many relationship with Students navigation property in Course class and Courses navigation property in Student class.

    Map method takes Action type delegate, hence, we can pass lambda expression wherein we will specify FK property name of Student (we start with Student entity, so it will be left table) and FK of Course table. ToTable will create StudentCourse table.

    This will create a new joining table StudentCourse with two Primary Keys which will also be Foreign Keys, as shown below:

    one-to-one relationship in code first

  • 相关阅读:
    将word转化为swf 进行如同百度文库的般阅读
    最大子数组问题——编程珠玑第八章
    为什么静态成员必须在类外初始化
    C++初始化列表
    异步消息总线hornetq学习-03客户端连接hornet进行jms消息的收发-非jndi方式连接
    [PLL][PM]锁相环模拟相位解调
    insertion sort
    SRM 581 D2 L2:SurveillanceSystem,重叠度
    JQuery(下)
    Ajax
  • 原文地址:https://www.cnblogs.com/neozhu/p/5203826.html
Copyright © 2011-2022 走看看