2013-12-21 54 views
-1

請檢查我以前的問題Here這種方法在MVC-5中是否正確?

正如我在我以前的問題中提到的,我使用Asp.Net身份進行用戶身份驗證。根據對前一個問題,我沒有這提供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); 
     } 
    } 

編寫這些代碼和用於創建數據庫創建初始遷移和更新,數據庫運行添加遷移命令後。 EF英孚成功創建包含以下表的數據庫:

  • _MigrationHistory
  • AspNetRoles
  • AspNetUserClaims
  • AspNetUserLogins
  • AspNetUserRoles
  • AspNetUsers
  • 學生
  • 教師
  • 聯繫

,我已經檢查學生和教師表包含4繼承IdentityUser基類,即列:

  • 編號
  • 用戶名
  • PasswordHash
  • SecurityStamp

Now我對我所做的是正確的做法感到困惑,或者我完全走錯了路。如果這是正確的方法,爲什麼EF生成AspNetUsers表?我只需要兩種類型的用戶,並且應該是學生和老師。這個AspNetUsers表的用途是什麼?

如果這不是正確的方法,請指導我在正確的道路上?

非常感謝提前。

+0

如果您只想要兩種類型的用戶,您爲什麼不創建2個角色作爲'Student'和'Teacher'並將這些角色分配給用戶。 –

+0

,因爲我必須爲每個用戶(學生和老師)保存不同的信息。所以對於兩個都有一個單獨的表 – user1740381

回答

2

您需要考慮更好的實體關係結構以節省studentteacher信息。 我想現在大家會認爲你需要在看到你的問題時使用UserRole。 這是因爲StudentTeacher都需要註冊,並且都屬於用戶帳戶。

我建議兩個不同的解決方案爲您提供:

1)外鍵ApplicationUser類添加兩個StudentTeacher,如下圖所示:

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實體了。

相關問題