【问题标题】:EF Inheritance Identity 2 access troubleEF 继承身份 2 访问问题
【发布时间】: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


【解决方案1】:

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

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

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.
}

【讨论】:

  • 不确定你在说什么。
  • 我无法将用户转换为客户我得到 InvalidCastException 当我尝试使用 Dbset 时我得到空上下文(那里没有用户)
  • 很可能,您也错误地创建了Customer 用户。特别是,如果您使用由 Identity 构建的 UserManager 控制器属性,那实际上是 UserManager<ApplicationUser> 的一个实例。如果您尝试使用它保存Customer,它将首先然后保存 向上转换为ApplicationUser。换句话说,您只会保存ApplicationUser 实例。您需要通过DbSet<Customer> 创建用户或创建UserManager<Customer> 的实例并使用它。
  • 在我尝试通过 DbSet 执行此操作之前,但它没有用户属性(Id、name、Surname、Emial)当我为我的应用程序创建模型时,我在那里使用 weblogs.asp.net/manavi/…我已经阅读了我只为基类 BillingDetail 定义了一个 DbSet。 Code First 将根据可达性约定在层次结构中查找其他类。所以我没有 DbSet
  • 接下来我尝试制作“私有只读 UserManager _userManager;”但出现 InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager`1[Project.Models.Customer]' 同时尝试激活 'Project.Controllers.CustomersController'。
猜你喜欢
  • 2018-11-18
  • 1970-01-01
  • 2021-07-24
  • 1970-01-01
  • 2018-02-01
  • 2014-11-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多