Eager Loading using UserManager with EF Core

asp.net-core c# entity-framework-core

Question

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?

1
12
10/3/2016 8:19:02 PM

Popular Answer

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.

1
5/20/2019 8:41:28 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow