【问题标题】:EF Core 6 Many to many table after scaffoldEF Core 6 脚手架后的多对多表
【发布时间】:2022-02-18 10:56:07
【问题描述】:

我从数据库制作了一个 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 不接受“Any”函数。

我怎样才能得到这个?

【问题讨论】:

    标签: .net-core entity-framework-core asp.net-core-webapi


    【解决方案1】:
    • Any(...)Enumerable class 提供的一种方法,因此您不能在单个 Student 上使用它(这显然是 不是 Enumerable 对象)。
    • 您的多对多关系配置可能缺少某些行,这是我的建议:
    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)))                                                                                                                                            
    

    【讨论】:

    • 谢谢,我会试试的。但是为什么脚手架会出错呢?也许数据库 FK 没有正确制作?我认为外键还可以
    【解决方案2】:

    我只是以你的代码为例,但这不是你在 EF 核心中设计实体的方式。

    尽管尝试跟随。

    var students 
     =_context.StudentCourse.Include("IdStudentNavigation").Where(x=>x.IdCourse == 1).Select(x => x.IdStudentNavigation).ToList(); 
    

    用你的课程 ID 替换一个。

    【讨论】:

    • 设计有什么问题?它是由数据库优先使用 dotnet 脚手架自动创建的
    猜你喜欢
    • 2016-10-10
    • 1970-01-01
    • 2021-06-14
    • 2015-01-08
    • 2022-11-07
    • 1970-01-01
    • 2022-08-04
    • 2022-10-17
    • 1970-01-01
    相关资源
    最近更新 更多