ASP.NET MVC 5 IdentityUser相关实体

时间:2014-07-01 23:26:50

标签: c# asp.net-mvc entity-framework asp.net-mvc-5

我有以下ApplicationUser实现:

public class ApplicationUser : IdentityUser
{
    public virtual Establishment Establishment { get; set; }
}

和企业实施:

public class Establishment
{
    [Key]
    public int ID { get; set; }

    public string Name { get; set; }

    public virtual ICollection<ApplicationUser> Users { get; set; }

    public virtual ICollection<Menu> Menus { get; set; }

    public virtual ICollection<Category> Categories { get; set; }
}

成就有很多用户,但用户只有一个机构。 我想通过当前用户ID获取当前的建立:

var currentUserEstablishmentId = userManager
    .FindById(User.Identity.GetUserId()).Establishment.ID;

var establishment = db.Establishments
    .Single(e => e.ID == currentUserEstablishmentId);

但它给我一个错误。属性currentUser的建立设置为null。

如何获得当前用户的当前建立?

谢谢你, 问候。

1 个答案:

答案 0 :(得分:3)

您是否尝试在ApplicationUser上添加EstablishmentID?

public class ApplicationUser : IdentityUser
{
    public virtual int EstablishmentID { get; set; }
    public virtual Establishment Establishment { get; set; }
}

然后您的查询变为:

var currentUser = userManager
    .FindById(User.Identity.GetUserId());

var establishment = db.Establishments
    .Single(e => e.ID == currentUser.EstablishmentID);