I have seen a similar post here, however, the answer didn't satisfy me.
I have 2 different types of users in my application. They both have different properties that I need to store in the database. For example my Student
user type has StudentId
and RegistedDate
fields whereas Instructor
user type has Major
field.
I am able to use Identity as authentication mechanism. I have created Student
and Instructor
roles that allow me to know who has access to what part of the app, I am good there.
I might be able to put common properties of different types such as Gender
in an ApplicationUser
class that extends the IdentityUser
However, I am not sure how I should store the user type-specific information. Should I extend this ApplicationUser
class once for each user type and have a single giant table or should I create sperate tables for each user type that has Id
in the ApplicationUser
class as foreign keys? Or should I do something entirely different?
I am sure many people faced and solved this problem before. What is the best practice to solve this?
I believe you should create separate tables for both the Student class and Instructor class with foreign keys to the ApplicationUser class.
Assuming you are using EF Core, here is an example:
public class Student
{
public int StudentId { get; set; }
// EF Core automatically creates a Foreign Key for you
public ApplicationUser ApplicationUser { get; set; }
}
public class Instructor
{
public int InstructorId { get; set; }
// EF Core automatically creates a Foreign Key for you
public ApplicationUser ApplicationUser { get; set; }
}
In your ApplicationDbContext class:
public DbSet<ApplicationUser> AppUsers { get; set; }
public DbSet<Student> Students { get; set; }
public DbSet<Instructor> Instructors { get; set; }