简体   繁体   中英

Eager loading in ASP.NET MVC 3 (Entity Framework)

I have a simple data structure

  • Employee
  • Employee has List
  • Education has University, Department
  • Department and University has Name as string (I mean Dept. and Univ. are also entity)

My question is, how can I eagerly load specific Employee's education list and each education's university and department information in Entity Framework.

In ASP.NET MVC tutorials there is a query like:

var viewModel = new InstructorIndexData();
viewModel.Instructors = db.Instructors
    .Include(i => i.OfficeAssignment)
    .Include(i => i.Courses.Select(c => c.Department))
    .OrderBy(i => i.LastName);

But my include takes only string parameter (with using System.Linq )

How can I solve this problem?

Thank you...

不是在System.Linq中,而是在System.Data.Entity中(因此使用System.Data.Entity),并且存在于EF 4.1中,但是我必须承认我不知道它出现在哪个版本中。

That snippet is for a new version of EF I thin. What 4.x version are you using?

You should be able to use the name(s) of the Navigation properties:

viewModel.Instructors = db.Instructors
    .Include("OfficeAssignment")
    .Include("Courses.Department")   // not so sure about this one
    .OrderBy(i => i.LastName);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM