从数据库获取ApplicationUser时如何包含相关实体?

时间:2019-03-11 08:57:59

标签: asp.net-mvc asp.net-identity asp.net-identity-2

我正在使用ASP.NET MVC Identity2。我已向ApplicationUser添加了自定义属性(地址):

public class ApplicationUser : IdentityUser<long, CustomUserLogin, CustomUserRole, CustomUserClaim>
{
    public long? ProfileAddressId { get; set; }

    // note that address is not virtual
    [ForeignKey("ProfileAddressId")]
    public Address ProfileAddress { get; set; }
}

Address是我的数据库中有一个单独的表。

现在在我的控制器中,我想获取用户(并加载其地址):

public ActionResult MyAction()
{
    var applicationUserManager = System.Web.HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>();
    var userId = User.Identity.GetUserId<long>();

    // this does not load address
    var user = applicationUserManager.FindById(User.Identity.GetUserId<long>());

}

Address不是virtual,并且我已在ApplicationDbContext中禁用了延迟加载:

[DbConfigurationType(typeof(MySqlEFConfiguration))]
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, CustomRole, long, CustomUserLogin, CustomUserRole, CustomUserClaim>
{
    public ApplicationDbContext() 
        : base("myDB")
    {
        // Lazy loading is enabled by default - disable lazy loading
        Configuration.LazyLoadingEnabled = false;
    }
    // more code...
 }

我不知道为什么未加载Address

如何扩展ApplicationUserManager以包括地址?还是可以将EF配置为始终渴望与负载相关的实体?

0 个答案:

没有答案