【发布时间】:2017-11-13 11:58:25
【问题描述】:
我正在尝试从ApplicationUser 获取 TPH 模型数据库继承:
public class Customer:ApplicationUser
{
public DateTime? AddeDateTime { get; set; }
public int? DietLenght { get; set; }
public int? ConsultationCount { get; set; }
public int? PlannedWeight { get; set; }
public int? Age { get; set; }
public int? Height { get; set; }
如何访问这些属性?我试过了:
var customerUser = await _context.Users.FirstOrDefaultAsync(x => x.Id == user.Id);
但我得到了ApplicationUser。而且ApplicationUser 没有要设置的属性,所以我不能那样做。
customerUser .ConsultationCount = model.ConsultationCount;
customerUser .DietLenght = model.DietLenght;
customerUser .Height = model.Height;
customerUser .Age = model.Age;
我也试过了:
var dataUser = user as Customer;
但它也不起作用。
需要它在我的应用中创建新用户
if (ModelState.IsValid)
{
var currentlyLogInUser = _userManager.GetUserAsync(HttpContext.User).Result;
var currentlyLogInUserId = currentlyLogInUser.Id;
var user = new ApplicationUser
{
UserName = model.Email,
Email = model.Email,
Name = model.Name,
Surname = model.Surname
};
var result = await _userManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
var resultRole = await _userManager.AddToRoleAsync(user, "Customers");
if (resultRole.Succeeded)
{
_logger.LogInformation("User created a new account with password.");
var customer = (Customer) user;
customer.DieticianId = currentlyLogInUserId;
customer.AddeDateTime = DateTime.Today;
customer.ConsultationCount = model.ConsultationCount;
customer.DietLenght = model.DietLenght;
customer.Height = model.Height;
customer.Age = model.Age;
_context.Users.Update(customer);
await _context.SaveChangesAsync();
}
return RedirectToLocal(returnUrl);
}
AddErrors(result);
}
// If we got this far, something failed, redisplay form
return View();
}
【问题讨论】:
标签: c# entity-framework asp.net-core asp.net-identity