Consultation in Relationship Many-to-Many with LINQ to Entities

Asked

Viewed 182 times

0

I created a database that has two tables called Teachers and Courses. A teacher can teach many courses and a course can have many teachers. I created the table Undergraduates to relate both. When using the Entity Framework wizard to do the mapping, it did not generate the entity class from the Cursosprofessors table. Instead, both classes have Hashsets that represent their Teacher and Course lists. The problem is that, in the application, when I try to enroll a Teacher in a Course or enroll a Course for a Teacher, it may be that the Teacher is already related to the Course and, in this case, an exception is made. The question is: how can I recover all the Teachers of a Course or vice versa using LINQ to Entities? If I can get them back, I can fix the problem of ambiguous inscriptions. Grateful from now on.

1 answer

0

Do the research and bring one Curso Code 1, already in the list will come all Professores related:

Cursos curso = db.Cursos.Find(1);

// contem a lista de professores do curso 1
ICollection<Professores> profLista = curso.Professores.ToList(); 

can also do otherwise Professores code 1 brings the Cursos related:

Professores prof = db.Professores.Find(1);

// contem a lista de cursos do professor 1
ICollection<Cursos> curList = prof.Cursos.ToList();

References:

  • It didn’t work... The list of courses is coming empty, even with a teacher who is enrolled in 2 courses.

  • @Ronyelago put your code in the question please, all the code you are using? is that the usual code

  • @Ronyelago check if the Configuration.LazyLoadingEnabled = false; place true for the lazy shipment.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.