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.
See Question&Answers more detail:os