【问题标题】:Is this approach correct in MVC-5?这种方法在 MVC-5 中是否正确?
【发布时间】:2014-01-10 05:27:30
【问题描述】:

请查看我之前的问题Here

正如我在上一个问题中提到的,我正在使用 Asp.Net Identity 进行用户身份验证。根据上一个问题中提供的the answer,我这样做了:

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

编写此代码并运行 Add-Migration 命令创建初始迁移和 Update-Database 创建数据库后。 EF 成功创建了包含以下表格的数据库:

  • _MigrationHistory
  • AspNetRoles
  • AspNetUserClaims
  • AspNetUserLogins
  • AspNetUserRoles
  • AspNet 用户
  • 学生
  • 教师
  • 联系方式

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

  • 身份证
  • 用户名
  • 密码哈希
  • 安全印章

现在我很困惑我所做的是正确的方法,或者我完全走上了错误的道路。如果它是正确的方法,那么为什么 EF 生成 AspNetUsers 表?我只想要两种类型的用户,应该是学生和教师。这个 AspNetUsers 表有什么用?

如果这不是正确的方法,请引导我走正确的道路??

非常感谢。

【问题讨论】:

  • 如果您只需要两种类型的用户,为什么不创建两个角色StudentTeacher 并将这些角色分配给用户。
  • 因为我必须为每个用户(学生和老师)保存不同的信息。所以对于两者都有一个单独的表

标签: c# asp.net-mvc asp.net-identity


【解决方案1】:

您需要考虑一个更好的实体关系结构来保存studentteacher 信息。 我想现在每个人看到你的问题时都会认为你需要使用UserRole。 那是因为StudentTeacher都需要注册,并且都属于User Account。

我为您推荐两种不同的解决方案:

1) 为 StudentTeacher 添加外键到 ApplicationUser 类,如下所示:

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

注册账号的时候就知道这个账号是老师的,只要填写老师信息,User表中的studentId就为NULL。

2) 创建附加信息实体用于存储StudentTeacher's 信息,将学生和教师的所有属性放在一起,如下所示:

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

那么您就不再需要 StudentTeacher 实体了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-23
    • 2018-01-18
    • 2020-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多