How express the many to many relationship with single parent table in EF 5

Dhana Krishnasamy

I have something like a Department, which can have many courses and students. students can be linked to many courses and courses could have many students registered to it. I also want student and courses to be linked to a department.

How can i express this in EF/Fluent API, I have tried these

        modelBuilder.Entity<Student>().HasRequired(s => s.Department);
        modelBuilder.Entity<Course>().HasRequired(u => u.Department);

        modelBuilder.Entity<Course>()
                    .HasMany(s => s.Students)
                    .WithOptional()
                    .WillCascadeOnDelete(false);
        modelBuilder.Entity<Course>()
                    .HasMany(u => u.Students)
                    .WithMany(s => s.Courses);
        modelBuilder.Entity<Student>()
                    .HasMany(s => s.Courses)
                    .WithOptional()
                    .WillCascadeOnDelete(false);

and I tried few other combinations as well, nothing worked and I get this error now

The navigation property 'Courses' declared on type 'Models.Student' has been configured with conflicting multiplicities.

Any hints/ help?

Update: The student class looks like this

public class Student
{
    [Key]
    public string Id { get; set; }

    [Required]
    public string SiteName { get; set; }

    public virtual Department Department { get; set; }

    public virtual ICollection<Course> Courses { get; set; }
}

and the Course:

    public class Course
{
    [Key]
    public string Id { get; set; }

    [Required]
    public string Name { get; set; }


    public virtual Department Department { get; set; }

    public virtual ICollection<Student> Students { get; set; }

}
Corey Adler

That's because in order to do a many-to-many like this you need to create a link table between the tables, which you might call StudentCourses. Then your Student and Course classes would have a collection of type StudentCourse. Here's how you would create the class (using Data Annotations):

public class StudentCourses 
{
    [ForeignKey("Student")]
    public int StudentID { get; set; }

    [ForeignKey("Course")]
    public int CourseID { get; set; }

    public virtual Student Student { get; set; }

    public virtual Course Course { get; set; }
}

Also, if you still want to be able to grab all students in a course (or all courses that a student is taking), you can do it easily using the NotMapped attribute for a public property to get it as follows:

For your Student class:

[NotMapped]
public List<Course> Courses {
  get { return StudentCourses.Select(sc => sc.Course); }
}

For your Course class:

[NotMapped]
public List<Student> Students {
  get { return StudentCourses.Select(sc => sc.Student); }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to populate a many-to-many relationship as table

From Dev

EF delete many to many relationship

From Dev

Build bidimensional table based on many-to-many EF relationship

From Dev

Many-to-many relationship between same table with extra column in EF

From Dev

EF6 Many to Many relationship, multiple tables to join table

From Dev

How to operate on the table many to many EF

From Dev

How to operate on the table many to many EF

From Dev

SQLAlchemy Many-to-Many Relationship on a Single Table Error: NoReferencedTableError

From Dev

Many-to-Many Relationship Against a Single, Large Table

From Dev

SQLAlchemy Many-to-Many Relationship on a Single Table Error: NoReferencedTableError

From Dev

Many-to-Many relationship in Access from a single table

From Dev

How to configure a One-to-Many relationship in EF

From Dev

How to create a many to many relationship with latest nightly builds of EF Core?

From Dev

how to write a Linq query with a EF code first Many to Many relationship

From Dev

Many-to-many relationship children exclusive to parent model. How?

From Dev

Recursive relationship on a many to many table

From Dev

SQL many to many relationship table

From Dev

Many to many relationship in the same table?

From Dev

Junction table/many to many relationship

From Dev

Dapper.net - how to map many to many relationship in single command

From Dev

how to find users with a single role in a many to many relationship?

From Dev

JPA or JPQL how to return single row from a many to many relationship

From Dev

Change name of generated Join table ( Many to Many ) - EF Core 5

From Dev

Many to Many Relationship EF Code First

From Dev

Many-to-Many relationship in WPF with EF and SQLite

From Dev

EF many-to-many relationship with specific scheam

From Dev

Many to many relationship with EF code first

From Dev

many-to-many relationship in mvc and EF

From Dev

Saving many to many relationship in EF 6.1

Related Related

  1. 1

    How to populate a many-to-many relationship as table

  2. 2

    EF delete many to many relationship

  3. 3

    Build bidimensional table based on many-to-many EF relationship

  4. 4

    Many-to-many relationship between same table with extra column in EF

  5. 5

    EF6 Many to Many relationship, multiple tables to join table

  6. 6

    How to operate on the table many to many EF

  7. 7

    How to operate on the table many to many EF

  8. 8

    SQLAlchemy Many-to-Many Relationship on a Single Table Error: NoReferencedTableError

  9. 9

    Many-to-Many Relationship Against a Single, Large Table

  10. 10

    SQLAlchemy Many-to-Many Relationship on a Single Table Error: NoReferencedTableError

  11. 11

    Many-to-Many relationship in Access from a single table

  12. 12

    How to configure a One-to-Many relationship in EF

  13. 13

    How to create a many to many relationship with latest nightly builds of EF Core?

  14. 14

    how to write a Linq query with a EF code first Many to Many relationship

  15. 15

    Many-to-many relationship children exclusive to parent model. How?

  16. 16

    Recursive relationship on a many to many table

  17. 17

    SQL many to many relationship table

  18. 18

    Many to many relationship in the same table?

  19. 19

    Junction table/many to many relationship

  20. 20

    Dapper.net - how to map many to many relationship in single command

  21. 21

    how to find users with a single role in a many to many relationship?

  22. 22

    JPA or JPQL how to return single row from a many to many relationship

  23. 23

    Change name of generated Join table ( Many to Many ) - EF Core 5

  24. 24

    Many to Many Relationship EF Code First

  25. 25

    Many-to-Many relationship in WPF with EF and SQLite

  26. 26

    EF many-to-many relationship with specific scheam

  27. 27

    Many to many relationship with EF code first

  28. 28

    many-to-many relationship in mvc and EF

  29. 29

    Saving many to many relationship in EF 6.1

HotTag

Archive