question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Explicit loading for list of entries: context.Entries(...)...Load()

See original GitHub issue

For explicit loading EF Core offers (from docs):

var blog = context.Blogs
        .Single(b => b.BlogId == 1);

context.Entry(blog)
        .Reference(b => b.Owner)
        .Load();

If I have, for expample, list of 10 entries, I need to 10 times call Reference(…).Load() in foreach, that generate 10 SQL queries to DB.

How about optimized method Entries():

var blogs = context.Blogs
        .Where(...)
        .ToList();

context.Entries(blogs)
        .Reference(b => b.Owner)
        .Load();

which make a single SQL query like: select .... where [BlogOwner].[BlogId] in (?, ?, ?, ?, ?, ?)

Sorry, but I have not found similar functionality. Thanks

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:19
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
Remleocommented, Jan 11, 2017

Sometimes may need to eager/explicit load a relationship after the parent model has already been retrieved. For example, this may be useful if I need to dynamically decide whether to load related models:

// This code is responsible for retrieve specific blog entities
// But it has no idea about inner logic in BlackBox.SomeMethod()
// That is why it dont load Navigations
var neededBlogs = context.Blogs.Where(....).ToList();
....
if (someDynamicCondition)
        BlackBox.SomeMethod(context, neededBlogs);

....

public class BlackBox {
        public static void SomeMethod(DbContext context, IEnumerable<Blog> blogs)
        {
            // There might be code that ensure Owner loading, 
            // because this method has no idea about is `blogs` was preloaded `Owners` or not.
            // Also `Loader` should be intelligent enough for load only empty Navigations
            // so calling this method multiple times is safe
            context.Entries(blogs).Reference(b => b.Owner).Load(); // dry code... my vision :)

            foreach (var blog in blogs)
            {
                if (!string.IsNullOrEmpty(blog.Owner.email) && someDynamicCondition)
                {
                    SendNotificationToOwner(blog.Owner.email, "Alert!");
                }
            }
        }
}

This code works:

var ids = context.ChangeTracker
    .Entries<Blog>()
    .Select(e => e.Property(b => b.BlogId))
    .ToList();
context.Owner.Where(o => ids.Contains(o.BlogId)).Load();

but code is not “dry”. where statement need to be hardcoded and match FK for Navigation-property. Context knows all about FK so it’s his “job” to load Navigation-properties properly.

Sorry for my english (

2reactions
divegacommented, Jan 27, 2017

To things you can try:

  • Use eager loading, i.e. Include(b => b.Owner)
  • Write a query like this (I haven’t actually tested it, but it should work):
var ids = context.ChangeTracker
    .Entries<Blog>()
    .Select(e => e.Entity.BlogId)
    .ToList();
context.Owner.Where(o => ids.Contains(o.BlogId)).Load();
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to do explicit loading for multiple entries in EF?
1 Answer. Entry method give you the control over an entity attached to the current context, so before use it the entity must...
Read more >
Explicit Loading Related Entities in EF 6 and EF Core
The Load() method executes the SQL query in the database to get the data and fill up the specified reference or collection property...
Read more >
Explicit Loading of Related Data - EF Core
Explicit loading of related data with Entity Framework Core. ... You can explicitly load a navigation property via the DbContext.Entry(.
Read more >
Explicit Loading in Entity Framework Core
Explicit Loading in EF Core is a technique we query and load the related entities with an explicit call. Explicit loading works very...
Read more >
Entity Framework performance improvement: [Section 1]
Explicit loading is a technique in Entity Framework (EF) that allows you to load related entities on-demand, as opposed to automatically loading ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found