Bright boot using UserManager with EF Core

There is currently an ApplicationUser class with some custom properties, for example:

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

I want the current registered user with a list of related data ( Content property).

In my controller, if I put:

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

I get a registered user, but without any related data. But if I extract the current user using ApplicationDbContext , as shown below, I can get 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 that doesn't look right to me!

Any idea?

+10
source share
1 answer

checking the source code of [UserManager][1] , GetUserAsync will eventually FindByIdAsync , which will be provided by the IUserStore implementation. Looking through the source code in the question, it is very likely to use EFCore as an implementation of IUserStore.

In the case of EFCore, it was mentioned here that Find cannot be combined with include , so I, the guest, what you did to make the download in your question, can actually fix it.

0
source

Source: https://habr.com/ru/post/1257656/


All Articles