I am creating a simple application for a university where a student can make some kind of request, which is then processed by an employee of a certain specialty.
I would like to use the default MVC5 authentication system and extend the ApplicationUser class using the TPH template. Therefore, I added general properties to ApplicationUser:
public class ApplicationUser : IdentityUser { [Required] public string FirstName { get; set; } [Required] public string LastName { get; set; } [Required] public string Email { get; set; } }
then I created two classes that inherit ApplicationUser:
public class Student : ApplicationUser { public string PersonalNumber { get; set; } public bool Monitor { get; set; } public virtual Group Group { get; set; } public virtual ICollection<Request> Requests { get; set; } } public class Employee : ApplicationUser { public virtual EmployeeSpeciality EmployeeSpeciality { get; set; } public virtual ICollection<Request> Requests { get; set; } }
Currently, I want both types of users to register as a base identifier and store both in the same table and in asp.net inheritance example
As I thought, it would be enough to initialize the user var in the AccountController, which then goes to the UserManager as a student or as an employee. But after trying to register as a student, I get this exception:
Exception Details: System.Data.SqlClient.SqlException: Invalid column name 'PersonalNumber'. Invalid column name 'Monitor'. Invalid column name 'EmployeeSpeciality_Id'. Invalid column name 'Group_Id'.
My context class:
public class EntityContext : IdentityDbContext { public EntityContext() : base("DbConnection") { } public DbSet<ApplicationUser> ApplicationUsers { get; set; } public DbSet<Student> Students { get; set; } public DbSet<Employee> Employees { get; set; } ... }
and part of the controller action:
public async Task<ActionResult> Register(RegisterViewModel model) { if (ModelState.IsValid) { var user = new Student() { UserName = model.UserName, FirstName = model.FirstName, LastName = model.LastName, Email = model.Email }; var result = await UserManager.CreateAsync(user, model.Password);
I tried setting ApplicationClass to an abstract class, but no luck. Any help would be appreciated.
UPDATE: The problem is not in the code itself. I simply did not delete (or update) the database after making these changes to the model. After that, everything works fine.