Is this approach correct in MVC-5?

2019-09-20 13:34发布

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.

1条回答
Animai°情兽
2楼-- · 2019-09-20 14:16

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.

查看更多
登录 后发表回答