脚手架后 EF Core 6 多对多 table
EF Core 6 Many to many table after scaffold
我从数据库制作了一个 dotnet ef 脚手架,生成的 类 是:
public partial class Course
{
public int Id { get; set; }
public string? Description { get; set; }
}
public partial class Student
{
public int Id { get; set; }
public string? Name { get; set; }
}
public partial class StudentCourse
{
public int? IdStudent { get; set; }
public int? IdCourse { get; set; }
public virtual Student? IdStudentNavigation { get; set; }
public virtual Course? IdCourseNavigation { get; set; }
}
我想获取 Student 的列表,其中 Course 的 id 是 X
我试过 _context.Student.Include("StudentCourse").Where(x=>x.Any(....) 但 Intellisense 不接受“任何”功能。
我怎样才能得到这个?
我只是以您的代码为例,但这不是您在 EF 核心中设计实体的方式。
虽然尝试跟随。
var students
=_context.StudentCourse.Include("IdStudentNavigation").Where(x=>x.IdCourse == 1).Select(x => x.IdStudentNavigation).ToList();
用您的课程 ID 替换一个。
- Any(...) 是 Enumerable class[=24 提供的方法=] 所以你不能在单个 Student 上使用它(这显然 不是 Enumerable 对象 )。
- 您的 many-to-many 关系配置可能缺少一些行,这是我的建议:
public partial class Course
{
public int Id { get; set; }
public string? Description { get; set; }
public List<StudentCourse> StudentCourses { get; set; }
}
public partial class Student
{
public int Id { get; set; }
public string? Name { get; set; }
public List<StudentCourse> StudentCourses { get; set; }
}
public partial class StudentCourse
{
public int? IdStudent { get; set; }
public int? IdCourse { get; set; }
public virtual Student? StudentNavigation { get; set; }
public virtual Course? CourseNavigation { get; set; }
}
在上下文文件中:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<StudentCourse>()
.HasOne(sc => sc.StudentNavigation)
.WithMany(s => s.StudentCourses)
.HasForeignKey(sc => sc.IdStudent);
modelBuilder.Entity<StudentCourse>()
.HasOne(sc => sc.CourseNavigation)
.WithMany(c => c.StudentCourses)
.HasForeignKey(sc => sc.IdCourse);
}
最后,您的查询可以是:
IEnumerable<Student> students = await _context.Students
.Include(s => s.StudentCourses)
.Where(s => s.StudentCourses.Any(sc => sc.IdCourse == X)))
我从数据库制作了一个 dotnet ef 脚手架,生成的 类 是:
public partial class Course
{
public int Id { get; set; }
public string? Description { get; set; }
}
public partial class Student
{
public int Id { get; set; }
public string? Name { get; set; }
}
public partial class StudentCourse
{
public int? IdStudent { get; set; }
public int? IdCourse { get; set; }
public virtual Student? IdStudentNavigation { get; set; }
public virtual Course? IdCourseNavigation { get; set; }
}
我想获取 Student 的列表,其中 Course 的 id 是 X
我试过 _context.Student.Include("StudentCourse").Where(x=>x.Any(....) 但 Intellisense 不接受“任何”功能。
我怎样才能得到这个?
我只是以您的代码为例,但这不是您在 EF 核心中设计实体的方式。
虽然尝试跟随。
var students
=_context.StudentCourse.Include("IdStudentNavigation").Where(x=>x.IdCourse == 1).Select(x => x.IdStudentNavigation).ToList();
用您的课程 ID 替换一个。
- Any(...) 是 Enumerable class[=24 提供的方法=] 所以你不能在单个 Student 上使用它(这显然 不是 Enumerable 对象 )。
- 您的 many-to-many 关系配置可能缺少一些行,这是我的建议:
public partial class Course
{
public int Id { get; set; }
public string? Description { get; set; }
public List<StudentCourse> StudentCourses { get; set; }
}
public partial class Student
{
public int Id { get; set; }
public string? Name { get; set; }
public List<StudentCourse> StudentCourses { get; set; }
}
public partial class StudentCourse
{
public int? IdStudent { get; set; }
public int? IdCourse { get; set; }
public virtual Student? StudentNavigation { get; set; }
public virtual Course? CourseNavigation { get; set; }
}
在上下文文件中:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<StudentCourse>()
.HasOne(sc => sc.StudentNavigation)
.WithMany(s => s.StudentCourses)
.HasForeignKey(sc => sc.IdStudent);
modelBuilder.Entity<StudentCourse>()
.HasOne(sc => sc.CourseNavigation)
.WithMany(c => c.StudentCourses)
.HasForeignKey(sc => sc.IdCourse);
}
最后,您的查询可以是:
IEnumerable<Student> students = await _context.Students
.Include(s => s.StudentCourses)
.Where(s => s.StudentCourses.Any(sc => sc.IdCourse == X)))