Please check my previous question Here.
As i mentioned in my previous question that i am using Asp.Net Identity for user authentication. According to the answer provided in the previous question i did this:
public class Student: IdentityUser
{
...
..... *other properties*
public int ContactId { get; set; }
public virtual Contact Contact { get; set; }
}
public class Teacher: IdentityUser
{
...
..... *other properties*
public int ContactId { get; set; }
public virtual Contact Contact { get; set; }
}
public class Contact
{
public int Id { get; set; }
...
..... *other properties*
}
public class MyContext : IdentityDbContext
{
public DbSet<Contact> Contacts { get; set; }
public MyContext()
: base("MyDatabase")
{ }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Student>().Map(m =>
{
m.MapInheritedProperties();
m.ToTable("Students");
});
modelBuilder.Entity<Teacher>().Map(m =>
{
m.MapInheritedProperties();
m.ToTable("Teachers");
});
//1 to 1 foreign key relationship b/w Student and Contact
modelBuilder.Entity<Student>().HasRequired(p => p.Contact).WithMany().HasForeignKey(p => p.ContactId);
//1 to 1 foreign key relationship b/w Teacher and Contact
modelBuilder.Entity<Teacher>().HasRequired(p => p.Contact).WithMany().HasForeignKey(p => p.ContactId);
base.OnModelCreating(modelBuilder);
}
}
After writing this code and running Add-Migration command for creating initial migration and Update-Database for creating database. The EF successfully created the database which contains the following tables:
- _MigrationHistory
- AspNetRoles
- AspNetUserClaims
- AspNetUserLogins
- AspNetUserRoles
- AspNetUsers
- Students
- Teachers
- Contacts
And i have checked Students and Teachers tables contain 4 inherited columns from IdentityUser base class and i.e.:
- Id
- UserName
- PasswordHash
- SecurityStamp
Now i am confused about what i have done is the correct approach or i am completely on a wrong path. If it is the correct approach than why EF generate AspNetUsers table ?. I want only 2 types of Users and that should be Student and Teacher. What is the use of this AspNetUsers table ?
And if this is not the correct approach than please guide me on the correct path ??
Many thanks in advance.
You need to think of a better entity relationships structure for saving
student
andteacher
information. I guess right now everybody will think you need to useUserRole
when they see your question. That's becauseStudent
andTeacher
both need to be registered, and both belong to User Account.I suggest two different solution for you:
1) Add foreign keys to ApplicationUser class for both
Student
andTeacher
, like below:When you register an account, you know the account is for teachers, just fill out teacher information, and the studentId in User table will be NULL.
2) Create Additional Information Entity for storing the
Student
andTeacher's
information, put all properties for Student and Teacher in it together, like below:Then you don't need
Student
andTeacher
Entities anymore.