Entity Framework retrieve objects based in condition

c# dbcontext entity-framework-6

Accepted Answer

You must continue to develop the linkages in your model. You are lacking the correct attributes in each of the many-to-many relationships between students and courses, as well as a joining table and the required model builder (context) settings.

To add your fluent settings, override your dbcontext model creation method:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        //Build many-to-many relationships
        modelBuilder.Entity<Student>()
                      .HasMany<Course>(c => c.Courses)
                      .WithMany(s => s.Student)
                      .Map(pe =>
                      {
                          pe.MapLeftKey("Student_ID");
                          pe.MapRightKey("Course_ID");
                          pe.ToTable("StudentCoursesTable");
                      });
    }

Fill up the gaps in your student class's courses:

    public class Students
    {
        public ICollection<Courses> Courses { get; set; } 
    }
0
1/30/2019 12:40:11 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow