Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
178 views
in Technique[技术] by (71.8m points)

c# - Is this approach correct in MVC-5?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...