【发布时间】:2015-11-06 23:22:07
【问题描述】:
我将 ASP.NET Identity 2.2.0 与 ASP.NET MVC 5.2.3 和 Entity Framework 6.1.2 一起使用。
我使用 ASP.NET Identity 和 Code First 向我的数据库添加了一个新属性及其对应的表,如下所示:
public class ApplicationUser
{
[ForeignKey("UserTypeId")]
public UserType Type { get; set;}
public int UserTypeId { get; set;}
}
public class UserType
{
[Key]
public int Id { get; set;}
public string Name { get; set; }
}
现在,通过一些行动,当我打电话时:
var user = UserManager.FindByNameAsync(userName);
它确实为用户提供了正确的UserTypeId,因为这是一个原语,但它没有获得ApplicationUser 类的UserType 属性。
如果我不使用此抽象,我将在 Entity Framework 中调用 LoadProperty<T> 或 Include 方法以在 ApplicationUser 上包含名为 Type(类型为 UserType)的导航属性或关系类。
如何使用 ASP.NET Identity 的 UserManager 做到这一点?我怀疑唯一的方法是在我的自定义 UserManager 派生类中重写此方法并自己做?
【问题讨论】:
-
您是否尝试过将您的
ApplicationUser类virtual的Type属性设为virtual? -
谢谢。做到了。非常感谢。如果您将其发布为答案,好吗? :-)
-
所以它无法代理它?
标签: entity-framework asp.net-identity asp.net-identity-3