2

I use ASP.NET Identity 2.0 in an MVC application and there is a default table called AspNetUsers in order to keep user data with the columns below:

AspNetUsers:

Id
Email
EmailConfirmed
PasswordHash
... etc

I know that I can add custom properties to this Entity as Name, Surname, etc. to this AspNetUsers table. However, there are 2 types of users in my project: Student and Professor. Therefore I need to use different properties as shown below:

Student:

Id
Name
Surname
Email
StudentNumber
Department

Professor:

Id
Name
Email
Surname
Profession
RoomNumber

At this point I have a problem when combining these entities and I do not know how to combine them. Students and Professors are stored in AspNetUsers table and I think there is no need to define the same data as Email on different tables and it is better to use only the different properties in the Student and Professor tables as StudentNumber, RoomNumber, etc. When a user login to the system I think to use AspNetUsers table first and then get the detail data for the current user from the Student or Professor table. Is there a better approach for this situation? I think ASP.NET Identity is commonly used and many people need to store different detail properties for different type of users. Any help would be appreciated.

2
  • What's wrong with the approach you describe? I do it myself and it works great Commented Jun 23, 2016 at 10:25
  • I just wanted to be sure if there is a better approach and if this approach are good. If you have idea, could you please have a look at 3 questions below and comment your opinion? Thanks. Commented Jun 23, 2016 at 11:02

2 Answers 2

3

Here is another possible solution, though it is up to you to determine if it is a good fit for your needs. You can you Table per Hierarchy (TPH) for your model. First make your user class abstract and put all common properties in there:

public abstract class ApplicationUser : IdentityUser
{
    //snip

    //Common properties
    public string Name { get; set; }
    public string Surname { get; set; }
}

Now create your different user type classes and inherit from the base class:

public class StudentUser : ApplicationUser
{
    public string StudentNumber { get; set; }
}

public class ProfessorUser : ApplicationUser
{
    public string Profession { get; set; }
}
Sign up to request clarification or add additional context in comments.

16 Comments

Actually it seems to be very good approach but, as I use EF Code First, I am not sure how can I use it. For example, when I create a Student record using StudentUser entity and add Name, SUrname, will EF add the Name, Surname to the ApplicationUser table as it add StudentNumber to StudentUser? Any idea?
Yes, all properties will be in the same user table, including a column called Discriminator that will have a value such as StudentUser in it for EF to determine the type.
Also, this is code only so is, by definition, code-first.
I will try it asap and inform you if it works. Voted+
I prefer this approach +1
|
2

Move the common properties to AspNetUsers table, i.e. your name, email, surname. In the same way the login details functionality will be the same for students and teachers alike, so are these tables. For the information that is specific to a teacher or professor that's where you need your specific tables. E.g.

public class AspNetUser
{ 
    // Normal properties

    public virtual Professor Professor { get; set; }

    public virtual Student Student { get; set; }
}

public class Professor 
{
    [Key]
    public int Id { get; set; }

    public virtual AspNetUser User { get; set; } // This is where you will be able to retrieve their name, email etc

    public string Profession { get; set; } 

    // Any other properties specific to a Professor
}

4 Comments

Thanks a lot. In that case I think there is no problem regarding to my approach. On the other and, I am wondering these points and I would be happy if you clarify me abut them >>>
1) I also add Type property to the AspNetUser table in order to determine that if the user Student or Professor. Then while creating a new user, I add a new record to the AspNetUser table and add another record to the Student or Professor table with the custom properties, is that true? 2) If the 1st is true, do you suggest to use transaction in EF? 3) When a user login to the system I look at the Type during authentication abd retrieve the detail data from the corresponding table. Is that true? Thanks...
@binary Unfortunately, these are all separate questions so you may want to ask new ones. However, do not create a Type property. In your AspNetUsers table you will have public virtual Professor and another for Student and then if one is null you will know which one it is.
Thanks for reply, but I do not understand what do you mean with your explanations about Type property. Could you please clarify a little bit more?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.