I have an AppUser class that inherits from IdentityUser and contains an Inventory object:
public class AppUser : IdentityUser
{
public InventoryModel? Inventory { get; set; }
}
I've been using this line to get the current user with all their properties:
var user = await _userManager.GetUserAsync(User);
Any primitive data types I put in AppUser can be accessed this way, but it seems to be lazy loading and not bringing the inventory along with it. I have manually queried my database and I see that the user I'm getting has an Inventory.InventoryId of 14 (primary key identifier for the Inventory class), but when I use the above statement to get the user, their Inventory is always null.
Is there some kind of ".Include()" that can be used with GetUserAsync? Is there another way I should be doing this?
For anyone with this same issue looking for a quick fix:
As Eldar suggested, I injected the ApplicationDbContext
and then did something like this:
//get lazy-loaded current user using the usermanager
var lazyUser = await _userManager.GetUserAsync(User);
//get current user with includes using the ApplicationDbContext
//and the Id from the current user I already grabbed
var user = await _context.AppUsers.Where(u => u.Id == lazyUser.Id)
.Include(u => u.Inventory)
.SingleOrDefaultAsync();
It's pretty inelegant but technically it accomplish what I wanted to accomplish.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.