Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF Core populate nested navigation property with Collection.LoadAsync

In EF Core, I'm populating a navigation property ; User.BookMarks with Collection.LoadAsync:

public async override Task<User> Fetch(string id)
{
    var user = await entities.FindAsync(id);
    await context.Entry(user).Collection(i => i.BookMarks).LoadAsync();
    return user;
}

However, BookMark also has a navigation property, BookMark.VgnItmEst which is not being populated. How do I populate that too?

like image 241
BeniaminoBaggins Avatar asked Feb 13 '26 13:02

BeniaminoBaggins


1 Answers

Not obvious directly from the docs, but you can insert Query method (available for both collection and reference navigation properties) call which

Returns the query that would be used by Load() to load entities referenced by this navigation property.

and apply as many Include / ThenInclude you want (similar to eager loading) before calling Load{Async}. e.g.

await context.Entry(user).Collection(i => i.BookMarks)
    .Query()
    .Include(e => e.VgnItmEst)
    .LoadAsync();
like image 67
Ivan Stoev Avatar answered Feb 15 '26 04:02

Ivan Stoev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!