AppUser包含类型为“实体”的属性,该属性包含其自己的嵌套属性。如何热切加载用户的所有属性,包括Entity对象中的属性? UserManager对象上没有Include
方法。
AppUser.cs
using Microsoft.AspNetCore.Identity;
using System.ComponentModel.DataAnnotations.Schema;
namespace MyApp.Models
{
public class AppUser : IdentityUser
{
public AppUser()
{
this.Entity = new Entity();
}
public string FirstName { get; set; }
public string LastName { get; set; }
[ForeignKey("EntityFK")]
public Entity Entity { get; set; }
}
}
Entity.cs
namespace MyApp.Models
{
public class Entity
{
public int EntityId { get; set; }
public string Name { get; set; }
public AppUser AppUser { get; set; }
}
}
EntityController.cs
using System.Threading.Tasks;
using MyApp.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
namespace MyApp.Controllers
{
[Authorize(Roles = "Entity")]
public class EntityController : Controller
{
private UserManager<AppUser> userManager;
public MyAppController(UserManager<AppUser> _userManager)
{
userManager = _userManager;
}
[HttpGet]
public async Task<IActionResult> Index()
{
/* How can eager loading be used to get the Entity and its properties? */
AppUser user = await userManager.GetUserAsync(HttpContext.User);
return View("~/Views/Entity/Index.cshtml", user);
}
}
}