简体   繁体   中英

Relationship loading in EF Core

Just met a problem with relationships loading. The problem is when I'm trying to access to any relationship and it's null. I have generic repository and is there any way to enable eager loading in it, not in the entity configuration? Tried lazy loading but it isn't necessary as for me.

When using Eager loading you can use the Include method to specify related data to be included in query results.

So, let's take a context of books that have many authors related to them. The books that are returned in the results will have their authors property populated with the related books.

using (var context = new BloggingContext())
{
   var books = context.Books
      .Include(book => book.Authors)
      .ToList();
 }

More about eager loading of related data - https://docs.microsoft.com/en-us/ef/core/querying/related-data/eager

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