How do I eagerly Include the child and grandchild elements of an entity in Entity Framework Code First?

前端 未结 3 1867
故里飘歌
故里飘歌 2020-12-05 12:48

Imagine three entities (Customer, Book, Author) related like this:

A Customer has many Books

A Book has one Author

I use that data to print a report

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-05 13:05

    There's an overload for Include that accepts a string which can denote the full path to any extra properties you need:

    var customers = db.Customers.Include("Books.Author");
    

    It looks strange because "Author" isn't a property on a collection of books (rather a property on each individual book) but it works. Give it a whirl.

提交回复
热议问题