EF继承身份2访问麻烦

时间:2017-11-13 11:58:25

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

我正在尝试从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();
        }

1 个答案:

答案 0 :(得分:0)

上下文的Users属性是DbSet<ApplicationUser>,因此它只会返回ApplicationUser个实例。您需要DbSet<Customer>才能获得Customer

或者,您可以在事后简单地转换为正确的类型。返回的实体仍然是Customer;它被ApplicationUser类型约束简单地上传到DbSet。结果你可以这样做:

if (user is Customer)
{
    var customer = (Customer)user;
    // work with `customer`
}

使用C#7的模式匹配,您可以将其简化为:

if (user is Customer customer)
{
    // work with `customer`
}

或者,如果您有多种不同的用户类型,则可以使用带有模式匹配的switch语句:

switch (user)
{
    case Customer customer:
        // work with `customer`
        break;
    case AnotherUserType another:
        // work with `another`
        break;
    // etc.
}