# Creating users with different roles using .Net Identity 2.0 within Entity Framework

I am using .Net Identity 2.0 with Entity Framework 6.0. I have a Person class inheriting IdentityUser. I have a Teacher (has additional Title property) and Student (has additional StudentNumber property) class inheriting from the Person class. I also have their corresponding roles in the database: Student and Instructor (or teacher).

I wonder if the code below is the most efficient way of creating users with specific roles, it seems to be a redundant but I could not figure a shorter way:

 gEchoLuDBContext _db = new gEchoLuDBContext();

var userStore = new UserStore<IdentityUser>(_db);
var userManager = new UserManager<IdentityUser>(userStore);

foreach (ListItem role in rb_Roles.Items)
{
if (role.Selected)
{
var user = new Person()
{
FirstName = txt_Firstname.Text,
LastName = txt_Lastname.Text,
Email = txt_Email.Text
};

if (role.Text == "Student")
{
user = new Student()
{
StudentNumber = " ",
FirstName = txt_Firstname.Text,
LastName = txt_Lastname.Text,
Email = txt_Email.Text
};
}
else if (role.Text == "Instructor")
{
user = new Teacher()
{
Title = "",
FirstName = txt_Firstname.Text,
LastName = txt_Lastname.Text,
Email = txt_Email.Text
};
}

if (result.Succeeded)
{
if (!result2.Succeeded)
{
lbl_Result.Text = "The user created successfully. But, the selected roles are not assigned!";
}
}
}
}


Well, you say that both Student and Teacher inherit from the base class Person. Considering that, and the fact that you never actually use either the Student or Teacher specifically in this code, why bother checking the Role at all? Your UserManager<IdentityUser> is taking in a person, not a Teacher or a Student.
Now, if I would happen to be wrong about that, I would suggest looking into the Factory Pattern. You could create a factory that takes in all of the fields from your GUI as arguments, and decides which type of Person to create based on the arguments it's recieved. It won't do away with all of the if...else if logic, but it will abstract it away and hide it from your GUI and make your code more testable.