'Entity Framework Core: load related data after object is queried

It is clear how to query an entity and include all the related data; e.g.:

using (var context = new BloggingContext())
{
    var blogs = context.Blogs
        .Include(blog => blog.Posts)
        .Include(blog => blog.Owner)
        .ToList();
}

REF

My question is, given an entity that is missing some related data, how I can ask EF to fetch its related data from DB?

For instance:

var blog = context.Blogs.Find(1); // get the blog with ID=1

// how can I do something like:
blog.Include(x => x.Posts);


Solution 1:[1]

context.Entry(blog).Collection(x => x.Posts).Load();
context.Entry(blog).Reference(x => x.Owner).Load();

According to this reference, as suggested by @GertArnold

Solution 2:[2]

As microsoft said, the retun dataType of Find() method is TEntity.

public virtual TEntity Find (params object[] keyValues);

and It means that Find() method hit the database and will fetch a TEntity. So you can't extend your query after Find() method. hence, If you wanna get related data in this query you must call Include() before any method which hit the database. as like as below query:

var blog = context.Blogs.Include(x => x.Posts).SingleOrDefault(x => x.Id == 1);

As a conclusion, The way that you expected to do with Includ() is not possible, but if you have to do this as @GertArnold said in comment, you can follow this way.

good luck.

Solution 3:[3]

try to use this

var blog = context.Blogs.Include(x => x.Posts).FirstOrDefault(x => x.Id == 1);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Hamed
Solution 2
Solution 3 Meysam