如何从Entity Framework Core建立的多对多关系中获取数据?

问题描述 投票:0回答:1

我从this tutorial建立了多对多关系

public class Student
{
    public int StudentId { get; set; }
    public string Name { get; set; }

    public IList<StudentCourse> StudentCourses { get; set; }
}

public class Course
{
    public int CourseId { get; set; }
    public string CourseName { get; set; }
    public string Description { get; set; }

    public IList<StudentCourse> StudentCourses { get; set; }
}

public class StudentCourse
{
    public int StudentId { get; set; }
    public Student Student { get; set; }

    public int CourseId { get; set; }
    public Course Course { get; set; }
}

我如何通过StudentID取得课程列表?

如何转换为IQueryable<Student>(需要asp.net吗?

谢谢。

c# asp.net-core entity-framework-core many-to-many
1个回答
2
投票

如果您只想获得学生正在上的所有课程,则有多种方法可以解决此问题。我猜您在数据库上下文中有一个DbSet<StudentCourse> StudentCourses条目。您可以简单地拨打context.StudentCourses.Include(x => x.Student).Where(entry => entry.CourseId == theIdYouWant).Select(entry => entry.Student)

© www.soinside.com 2019 - 2024. All rights reserved.