LINQ实体对EF的多对多关系

时间:2021-09-12 02:16:09

I'm using ASP.NET MVC4 EF CodeFirst.

我正在使用ASP.NET MVC4 EF CodeFirst。

Need help to write LINQ (to entities) code in Index action to get collection of Courses which are attended by selected student. The relationship is many to many with join table with payload.

需要帮助在Index动作中编写LINQ(to entities)代码以获取所选学生参加的课程集合。连接表与有效负载的关系是多对多的。

//StudentController
//-----------------------

public ActionResult Index(int? id)
{
    var viewModel = new StudentIndexViewModel();
    viewModel.Students = db.Students;

    if (id != null)
    {
        ViewBag.StudentId = id.Value;
        // *************PROBLEM IN LINE DOWN. HOW TO MAKE COURSES COLLECTION? 
        viewModel.Courses = db.Courses
            .Include(i => i.StudentsToCourses.Where(t => t.ObjStudent.FkStudentId == id.Value));
    }


    return View(viewModel);
}

The error I got is:

我得到的错误是:

The Include path expression must refer to a navigation property defined on the type. Use dotted paths for reference navigation properties and the Select operator for collection navigation properties.

I have modeles (the third one is for join table with payload):

我有模型(第三个是与有效负载的连接表):

//MODEL CLASSES
//-------------

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

    public virtual ICollection<StudentToCourse> StudentsToCourses { get; set; }
}

public class Course
{
    public int CourseId { get; set; }
    public string Title { get; set; }

    public virtual ICollection<StudentToCourse> StudentsToCourses { get; set; }
}

public class StudentToCourse
{
    public int StudentToCourseId { get; set; }
    public int FkStudentId { get; set; }
    public int FkCourseId { get; set; }
    public string Classroom { get; set; }

    public virtual Student ObjStudent { get; set; }
    public virtual Course ObjCourse { get; set; }
}

Then, here is modelview I need to pass to view

然后,这里是模型视图,我需要传递给视图

//VIEWMODEL CLASS
//---------------

public class StudentIndexViewModel
{
    public IEnumerable<Student> Students { get; set; }
    public IEnumerable<Course> Courses { get; set; }
    public IEnumerable<StudentToCourse> StudentsToCourses { get; set; }
}

1 个解决方案

#1


1  

EF does not support conditional include's. You'll need to include all or nothing (ie no Whereinside the Include)

EF不支持条件包含。你需要包括所有或不包括(即包括在内的没有)

If you need to get the data for just certain relations, you can select it into an anonymous type, something like (the obviously untested);

如果您需要获取某些关系的数据,可以将其选择为匿名类型,例如(显然未经测试);

var intermediary = (from course in db.Courses
                    from stc in course.StudentsToCourses
                    where stc.ObjStudent.FkStudentId == id.Value
                    select new {item, stc}).AsEnumerable();

Obviously, this will require some code changes, since it's no longer a straight forward Course with a StudentsToCourses collection.

显然,这将需要一些代码更改,因为它不再是一个带有StudentsToCourses集合的直接课程。

#1


1  

EF does not support conditional include's. You'll need to include all or nothing (ie no Whereinside the Include)

EF不支持条件包含。你需要包括所有或不包括(即包括在内的没有)

If you need to get the data for just certain relations, you can select it into an anonymous type, something like (the obviously untested);

如果您需要获取某些关系的数据,可以将其选择为匿名类型,例如(显然未经测试);

var intermediary = (from course in db.Courses
                    from stc in course.StudentsToCourses
                    where stc.ObjStudent.FkStudentId == id.Value
                    select new {item, stc}).AsEnumerable();

Obviously, this will require some code changes, since it's no longer a straight forward Course with a StudentsToCourses collection.

显然,这将需要一些代码更改,因为它不再是一个带有StudentsToCourses集合的直接课程。