Further Extension of the ApplicationUser Class in the ASP.NET MVC5 Authentication System

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.

+8
entity-framework asp.net-mvc-5 asp.net-identity tph
source share
2 answers

@Dragonheart: I tried this reprogramming and it will work fine if you remove DBSet declarations in your context. IdentityDbContext will process your TPH template and add a Discriminator column to the table to differentiate child classes.

+1
source share

Since ApplicationUser inherits from IdentityUser , remove it from the DbContext class. On the other hand, there is no need to create an abstract class (you can create it if you do not want to create a user, except for the Student or Employee classes. For more information, you can see the table for the hierarchy .

For the Register part, try something like this:

 Student user = new Student { UserName = model.UserName, FirstName = model.FirstName, LastName = model.LastName, Email = model.Email }; var result = UserManager.Create(user, model.Password); 

Hope this helps ...

0
source share

All Articles