locked
entity framework 7: how to configure many to many relationship? RRS feed

  • Question

  • User-1761689277 posted

    i am using entity framework 7 beta.

    i could not find how to configure many to many relationship between entities. only one to one and one to many is available.

    could anyone help me and show me some demo codes?

    many thanks!

    Thursday, July 23, 2015 5:26 AM

Answers

  • User-84896714 posted

    Hi kennywangjin,

    Thank you for your post. You could use two one-many instead. Code below is for your reference.

    class Student
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public ICollection<StudentCourse> StudentCourses { get; set; }
    }
    
    class StudentCourse
    {
        public int StudentID { get; set; }
        public Student Student { get; set; }
    
        public int CourseID { get; set; }
        public Course Course { get; set; }
    }
    
    class Course
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public ICollection<StudentCourse> StudentCourses { get; set; }
    }

    Best Regards,
    Wang Li

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 23, 2015 10:13 PM

All replies

  • User-821857111 posted

    You configure it in the same way as you would if you were using EF 6. 

    http://www.entityframeworktutorial.net/code-first/configure-many-to-many-relationship-in-code-first.aspx

    Thursday, July 23, 2015 6:31 AM
  • User-84896714 posted

    Hi kennywangjin,

    Thank you for your post. You could use two one-many instead. Code below is for your reference.

    class Student
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public ICollection<StudentCourse> StudentCourses { get; set; }
    }
    
    class StudentCourse
    {
        public int StudentID { get; set; }
        public Student Student { get; set; }
    
        public int CourseID { get; set; }
        public Course Course { get; set; }
    }
    
    class Course
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public ICollection<StudentCourse> StudentCourses { get; set; }
    }

    Best Regards,
    Wang Li

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 23, 2015 10:13 PM