我正在使用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配置为始终渴望与负载相关的实体?