You need to think of a better entity relationships structure for saving student
and teacher
information.
I guess right now everybody will think you need to use UserRole
when they see your question.
That's because Student
and Teacher
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
and Teacher
, like below:
public class ApplicationUser : IdentityUser
{
public int ContactId { get; set; }
public int? StudentId { get; set; }
public int? TeacherId { get; set; }
[ForeignKey("ContactId")]
public virtual Contact Contact { get; set; }
[ForeignKey("StudentId")]
public virtual Student Student { get; set; }
[ForeignKey("TeacherId")]
public virtual Teacher Teacher { get; set; }
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection")
{
}
public DbSet<Contact> Contacts { get; set; }
public DbSet<Student> Student { get; set; }
public DbSet<Teacher> Teacher { get; set; }
}
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
and Teacher's
information, put all properties for Student and Teacher in it together, like below:
public class ApplicationUser : IdentityUser
{
public int ContactId { get; set; }
public int AdditionalInfoId { get; set; }
[ForeignKey("ContactId")]
public virtual Contact Contact { get; set; }
[ForeignKey("AdditionalInfoId")]
public virtual AdditionalInfo AdditionalInfo { get; set; }
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection")
{
}
public DbSet<Contact> Contacts { get; set; }
public DbSet<AdditionalInfo> AdditionalInfo { get; set; }
}
Then you don't need Student
and Teacher
Entities anymore.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…