gpt4 book ai didi

asp.net-identity - EF7 身份未加载用户扩展属性

转载 作者:行者123 更新时间:2023-12-03 07:14:06 24 4
gpt4 key购买 nike

我有一个扩展的 IdentityUser 类,其中包含对数据库上另一个实体的引用,但每当我尝试使用 UserManager 获取用户时,引用的实体总是为空:

我的 User 类的实现

public class Usuario : IdentityUser
{
public int ClienteID { get; set; }
public virtual Cliente Cliente { get; set; }
}

使用用户引用的属性的 Controller

[Authorize]
[HttpGet]
public async Task<Direccion> GET()
{
var usuario = await UserManager.FindByNameAsync(Context.User.Identity.Name);
// Cliente will always be null
return usuario.Cliente.Direccion;
}

我还尝试从引用中删除 virtual 关键字,以便延迟加载,但我不确定这是否已在 EF7 上实现。

关于如何实现这一目标有什么想法吗?

最佳答案

我也遇到了这个问题,并在EF Core Documentation的帮助下解决了它。 。您需要使用 Include 方法来获取要填充的相关数据。就我而言:

实体类:

public class ServiceEntity
{
public int ServiceId { get; set; }
public int ServiceTypeId { get; set; }

public virtual ServiceTypeEntity ServiceType { get; set; }

}

访问我的 DbContext 对象:

public ServiceEntity GetById(int id)
{
var service = this.DbClient.Services.Include(s => s.ServiceType).Where(c => c.ServiceId == id).FirstOrDefault();

return service;
}

我还必须将该属性设置为“虚拟”。我的理解是 EF Core 需要能够覆盖相关对象的属性才能填充值。

此外,截至本文发布之日,EF Core 尚不支持延迟加载,仅支持预加载和显式加载。

希望这有帮助!

关于asp.net-identity - EF7 身份未加载用户扩展属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30875569/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com