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"?
DbSet<Student> Students
andDbSet<Tutor> Tutors
in the dbcontext. Then you can use the_context.Tutors.ToList()
to get the information.User
in database? how is your DbCOntext? Does it inherit from theIdentityDbContext<User>
? And then you can find the extra columns with default IdentityUser columns indbo.AspNetUsers
table.