We have the following entities:
- Student: Represents a student.
- Major: Represents the student's field of study.
- Course: Represents a course offered in a major.
- StudentCourse: Represents the enrollment of a student in a course
And, Entity models are as follows:
public class Student
{
public int StudentId { get; set; }
public string Name { get; set; }
public int MajorId { get; set; }
public Major Major { get; set; }
public ICollection<StudentCourse> StudentCourses { get; set; }
}
public class Major
{
public int MajorId { get; set; }
public string Title { get; set; }
public ICollection<Course> Courses { get; set; }
}
public class Course
{
public int CourseId { get; set; }
public string Name { get; set; }
public int MajorId { get; set; }
public Major Major { get; set; }
public ICollection<StudentCourse> StudentCourses { get; set; }
}
public class StudentCourse
{
public int StudentId { get; set; }
public int CourseId { get; set; }
public Student Student { get; set; }
public Course Course { get; set; }
}
For fetch all course names that a specific student is enrolled in, we can use following statement:
var courseNames = await _context.StudentCourses
.Where(sc => sc.StudentId == studentId)
.Include(sc => sc.Course)
.Select(sc => sc.Course.Name)
.ToListAsync();
Seyed Hamed Vahedi
Tue, 4 November, 2025