0

I have a parent class "User" which inherits from "IdentityUser":

public class User : IdentityUser
{
    public string Lastname { get; set; }
    public string Name { get; set; }
    public string Password { get; set; }
    public string RoleId { get; set; }
    public IdentityRole Role { get; set; }
}

At the same time, the "User" class is the parent of the following classes "Student" and "Tutor":

public class Student : User
{
    public List<Tutor> Tutors { get; set; }
    public Student()
    {
        Tutors = new List<Tutor>();
    }

}

public class Tutor : User
{
    public string Description { get; set; }
    public string Experience { get; set; }
}

In the controller I create a user:

    public async Task<IActionResult> RegisterUser(RegisterUser model)
    {
        if (ModelState.IsValid)
        {
            IdentityRole role = _context.Roles.FirstOrDefault(r => r.Name == model.Role);

            User user = new User { Email = model.Email, UserName = model.Email, Name = model.Name, RoleId = role.Id, Role = role };


            var result = await _userManager.CreateAsync(user, model.Password);
            if (result.Succeeded)
            {
                await _userManager.AddToRoleAsync(user, model.Role);
                await _signInManager.SignInAsync(user, false);


                var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
                var callbackUrl = Url.Action(
                    "ConfirmEmail",
                    "User",
                    new { userId = user.Id, code = code },
                    protocol: HttpContext.Request.Scheme);

                Emailer.SendEmailAsync(model.Email, "Confirm your account",
                    $"Confirm the mail <a href='{callbackUrl}'>link</a>");
                

                return Content("Confirm the mail");

            }
            else
            {
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
            }
            
        }
        return View(model);
    }

Later I need to get the user based on their role. Example:

    public async Task<IActionResult> AccountHomePage()
    {
        User user = await _userManager.FindByNameAsync(User.Identity.Name);
        user.Role = _context.Roles.FirstOrDefault(r => r.Id == user.RoleId);
        if (user.Role.Name == "teacher")
        {
            //Tutor tutor = _context.BD_Tutors.FirstOrDefault(t => t.Id == user.Id);
            return RedirectToAction("AccountHomePageTutor", "Account");
        }
        else if(user.Role.Name == "student")
        {

            //Student student = _context.BD_Students.FirstOrDefault(t => t.Id == user.Id);
            return RedirectToAction("AccountHomePageTutor", "Account");
        }

        return NotFound();
        
    }

    [HttpGet]
    [Authorize(Roles = "teacher")]
    public async Task<IActionResult> AccountHomePageTutor()
    {
        User user = await _userManager.FindByNameAsync(User.Identity.Name);

        

        if (user != null)
        {
            //Error
            //The table in the database is created
            Tutor tutor = (Tutor)_context.Users.FirstOrDefault(t => t.Id == user.Id);

            return View(tutor);
        }

        return NotFound();
    }

    [HttpGet]
    [Authorize(Roles = "student")]
    public async Task<IActionResult> AccountHomePageStudent()
    {
        User user = await _userManager.FindByNameAsync(User.Identity.Name);
        if (user != null)
        {
            //Not convenient, but a working option
            Student student = new Student { Id = user.Id, Name = user.Name, Email = user.Email, Avatar = user.Avatar, Phone = user.Phone, RoleId = user.RoleId, Lastname = user.Lastname, Role = user.Role };
            return View(student);
        }

           
        return NotFound();
    }

How can I get information from the database for models "Student" and "Tutor"?

8
  • Hi @Artem, be sure you create the property DbSet<Student> Students and DbSet<Tutor> Tutors in the dbcontext. Then you can use the _context.Tutors.ToList() to get the information.
    – Rena
    Commented Mar 21, 2023 at 5:23
  • Hello. I have these fields created. But when I search, it does not find users. Maybe I'm creating a user wrong?
    – Artem
    Commented Mar 21, 2023 at 7:55
  • Hi @Artem, you mean you cannot find the User in database? how is your DbCOntext? Does it inherit from the IdentityDbContext<User>? And then you can find the extra columns with default IdentityUser columns in dbo.AspNetUsers table.
    – Rena
    Commented Mar 21, 2023 at 8:28
  • The second option. I have a user created in "User". But I can't get extra columns from "dbo.AspNetUsers"
    – Artem
    Commented Mar 21, 2023 at 9:52
  • for the model "Tutor"
    – Artem
    Commented Mar 21, 2023 at 9:58

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.