EF7身份未加载用户扩展属性 [英] EF7 Identity not loading User extended properties

查看:78
本文介绍了EF7身份未加载用户扩展属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an extended IdentityUser class which contains a reference to another entity on my DB, but whenever I try to get the User with the UserManager, the referenced entity always comes empty:

我对User类的实现

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

在用户上使用引用属性的控制器

A controller that uses the referenced property on the user

[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上是否已实现.

I also tried removing the virtual keyword from the reference, so that it is lazy loaded, but I'm not sure that is already implemented on EF7.

关于如何实现这一目标的任何想法?

Any ideas on how to achieve this?

推荐答案

我也遇到了这个问题,并在

I had this issue as well and solved it with the help of the EF Core Documentation. You need to use the Include method to get the related data to populate. In my case:

实体类:

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

    public virtual ServiceTypeEntity ServiceType { get; set; } 
     
}

访问我的DbContext对象:

Accessing my DbContext object:

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

    return service;
}

我必须将媒体资源设置为虚拟"也一样我的理解是,EF Core需要能够覆盖相关对象的属性才能填充值.

I had to make the property "virtual" as well. My understanding is EF Core needs to be able to override the properties of the related object in order to populate the values.

自发布之日起,EF Core还不支持延迟加载,仅支持快速加载和显式加载.

Also, as of this post date, EF Core does not yet support Lazy Loading, only Eager Loading and Explicit Loading.

希望这会有所帮助!

这篇关于EF7身份未加载用户扩展属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆