Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eager Loading using UserManager with EF Core

Currently have ApplicationUser class with some custom properties, like:

public class ApplicationUser : IdentityUser
{
    public string Name { get; set; }
    public List<Content> Content { get; set; }
}

I'd like to get the current logged user with the list of related data (Content property).

In my controller, if I put:

Applicationuser user = await _userManager.GetUserAsync(HttpContext.User);

I get the logged user, but without any related data. But, if I retrieve the current user using the ApplicationDbContext, like below, I can retrieve the related data:

ApplicationUser user = await _userManager.GetUserAsync(HttpContext.User);
ApplicationUser userWithContent = _context.Users.Include(c => c.Content).Where(u => u.Id == user.Id).ToList();

But this doesn't appear correctly for me!

Any idea?

like image 558
Roberto Correia Avatar asked Oct 03 '16 17:10

Roberto Correia


1 Answers

checking the source code of [UserManager][1], GetUserAsync will end up calling FindByIdAsync, which will be provided by an IUserStore implementation. Looking the source code in the question, very likely using EFCore as the IUserStore implementation.

In case of EFCore, it was mention here that Find cannot be combined with include, so I guest what've you done to do eager loading in your question, may actually correct.

like image 182
Riza Avatar answered Sep 23 '22 06:09

Riza