如何访问对其他对象的引用的appuser属性? (asp.net/efcore)

发布于 2025-02-13 06:27:49 字数 467 浏览 2 评论 0原文

我有一个从IdentityUser继承并包含一个清单对象的Appuser类:

public class AppUser : IdentityUser
    {
        public InventoryModel? Inventory { get; set; }
    }

我一直在使用此行来使当前用户具有其所有属性:

var user = await _userManager.GetUserAsync(User);

我可以以这种方式访问​​Appuser中的任何原始数据类型,但似乎要懒惰,不要随身携带库存。我已经手动查询了我的数据库,看到我得到的用户有一个库存。14(库存类的主要密钥标识符),但是当我使用上述语句获取用户时,他们的库存始终为null 。

是否有某种可以与getuserasync一起使用的“ .include()”?我应该这样做的另一种方式吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

回忆追雨的时光 2025-02-20 06:27:49

对于任何同样问题的人来说,寻找快速解决方案:

正如Eldar建议的那样,我注入了applicationdbcontext,然后做了这样的事情:

//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();

它非常不高,但是从技术上讲,它可以完成我想完成的工作。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文