.Net Core获取ApplicationUser的对象

时间:2017-06-29 19:14:53

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

我已经为标准项目的ApplicationUser添加了一些特定的属性。 一些添加的属性是自定义类。 由于我使用EntityFramework,它为用户创建一个dbtable,为每个自定义类创建一个。 我将属性添加到我的ManageController和Views,并将这些属性添加到特定的dbtable工作,但我无法访问它们。在dbo.AspNetUsers中添加了一个列,在属性+ ID之后调用(在我的示例中为“NameID”)。

现在,如果我在ManageController中加载用户,则会加载每个普通属性,但自定义属性为空。

我的问题是,如何加载自定义对象(实际存储在另一个表中)。

ApplicationUser.cs:

namespace refProject.Models
{
    public class ApplicationUser : IdentityUser
    {
        public Name Name { get; set; }
    }
}

ManageController.cs

//other usings
using refProject.Models;
using refProject.Models.ManageViewModels;

namespace refProject.Controllers
{
    [Authorize]
    public class ManageController : Controller
    {
        private readonly UserManager<ApplicationUser> _userManager;
        //other managers

        public ManageController(
          UserManager<ApplicationUser> userManager,
          //other managers
          )
        {
            _userManager = userManager;
            //other managers
        }

        //
        // GET: /Manage/Index
        [HttpGet]
        public async Task<IActionResult> Index(ManageMessageId? message = null)
        {
            ViewData["StatusMessage"] =
                message == ManageMessageId.ChangeNameSuccess ? "Your name has been changed."
                : message == ManageMessageId.SetNameSuccess ? "Your name has been set."
                : "";

            var user = await GetCurrentUserAsync();
            if (user == null)
            {
                return View("Error");
            }
            var model = new IndexViewModel
            {
                //other Properties
                //
                //
                // THIS ONE IS NULL
                //
                //        
                Name = user.Name
                //other Properties
            };
            return View(model);
        }

        // GET: /Manage/ChangeName
        [HttpGet]
        public IActionResult ChangeName()
        {
            return View();
        }

        //
        // POST: /Manage/ChangeName
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> ChangeName(ChangeNameViewModel model)
        {
            if(!ModelState.IsValid)
            {
                return View(model);
            }
            var user = await GetCurrentUserAsync();
            if(user != null)
            {
                Name NewName = new Name();
                NewName.FirstName = model.NewFirstName;
                NewName.LastName = model.NewLastName;

                user.Name = NewName;

                IdentityResult result = await _userManager.UpdateAsync(user);
                if (result.Succeeded)
                {
                    return RedirectToAction(nameof(Index), new { Message = ManageMessageId.ChangeNameSuccess });
                }
                AddErrors(result);
                return View(model);
            }
            return RedirectToAction(nameof(Index), new { Message = ManageMessageId.Error });
        }

        //
        // GET: /Manage/SetName
        [HttpGet]
        public IActionResult SetName()
        {
            return View();
        }

        //
        // POST: /Manage/SetName
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> SetName(SetNameViewModel model)
        {
            if(!ModelState.IsValid)
            {
                return View(model);
            }

            var user = await GetCurrentUserAsync();
            if(user != null)
            {
                Name NewName = new Name();
                NewName.FirstName = model.NewFirstName;
                NewName.LastName = model.NewLastName;

                user.Name = NewName;

                IdentityResult result = await _userManager.UpdateAsync(user);
                if(result.Succeeded)
                {
                    return RedirectToAction(nameof(Index), new { Message = ManageMessageId.SetNameSuccess });
                }
                AddErrors(result);
                return View(model);
            }
            return RedirectToAction(nameof(Index), new { Message = ManageMessageId.Error });
        }

        #region Helpers

        private void AddErrors(IdentityResult result)
        {
            foreach (var error in result.Errors)
            {
                ModelState.AddModelError(string.Empty, error.Description);
            }
        }

        public enum ManageMessageId
        {
            Error,
            ChangeNameSuccess,
            SetNameSuccess,
        }

        private Task<ApplicationUser> GetCurrentUserAsync()
        {
            return _userManager.GetUserAsync(HttpContext.User);
        }

        #endregion
    }
}

Name.cs

namespace refProject.Models
{
    public class Name
    {
        public int ID { get; set; }
        public string fTitle { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string lTitle { get; set; }

        public override string ToString()
        {
            return fTitle + " " + FirstName + " " + LastName + " " + lTitle;
        }
    }
}

1 个答案:

答案 0 :(得分:5)

这是一个已知问题。它不被视为错误,而是设计决策。

推荐的方法是通过DbContext而不是UserManager实施来访问用户。

  

“只是为了添加更多细节:作为性能优化,ASP.NET Core Identity目前仅根据需要加载与用户或角色相关的实体以满足API调用。即它不会加载相关实体(不是甚至是内置的那些)急切地对FindByName()这样的方法调用进行调用,因为只需要返回根来查找方法。

     

此时,建议针对DbContext发出查询   加载相关数据的方法。如果你想从中抽象出来   应用程序代码,您可以扩展Identity存储和管理器   用于添加检索和返回自定义相关方法的类   数据“。

     

Comment link

您可以按如下方式更改GetCurrentUserAsync方法:

private ApplicationUser GetCurrentUserAsync()
{
    return _userManager.Users.Include(x => x.Name).FirstOrDefault(x => x.Id == _userManager.GetUserId(User));
}