这是办法,MVC-5是否正确? [英] Is this approach correct in MVC-5?

查看:115
本文介绍了这是办法,MVC-5是否正确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请检查我的previous问题这里

Please check my previous question Here.

我在previous疑问,我使用Asp.Net身份进行用户认证提及。据<一个href=\"http://stackoverflow.com/questions/20716711/how-to-do-handle-login-logoff/20717547#20717547\">the答案在previous问题,我这样做提供:

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);
        }
    }

写这篇code和用于创建数据库创建初始迁移和更新,数据库运行添加迁移命令后。 EF英孚成功创建包含以下表的数据库:

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

  • 学生

  • 教师

  • 联系人

和我检查了学生和教师表包含4列继承从IdentityUser基类即:

And i have checked Students and Teachers tables contain 4 inherited columns from IdentityUser base class and i.e.:


  • 标识

  • 用户名

  • PasswordHash

  • SecurityStamp

现在我很困惑什么,我所做的是正确的做法还是我完全是一个错误的道路上。如果这是正确的做法不是为什么EF产生的 AspNetUsers 表?我想只有2类型的用户这应该是学生和教师。什么是使用这种AspNetUsers表?

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 ??

提前很多感谢。

推荐答案

您需要考虑一个更好的实体关系结构保存学生老师信息。
我猜的没错,现在大家会认为你需要使用的UserRole 当他们看到你的问题。
这是因为学生教师都需要进行登记,并同属用户帐户。

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)外键添加到ApplicationUser类为学生教师,象下面这样:

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; } 
}

当您注册一个帐号,你就知道该帐户是为教师,只需填写教师信息,以及studentId用户表将是NULL。

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)创建附加信息实体的学生教师信息,把对学生和教师的所有属性在它在一起,就像如下:

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; }
}

然后,你不需要学生教师实体了。

这篇关于这是办法,MVC-5是否正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆