請檢查我以前的問題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表的用途是什麼?
如果這不是正確的方法,請指導我在正確的道路上?
非常感謝提前。
如果您只想要兩種類型的用戶,您爲什麼不創建2個角色作爲'Student'和'Teacher'並將這些角色分配給用戶。 –
,因爲我必須爲每個用戶(學生和老師)保存不同的信息。所以對於兩個都有一個單獨的表 – user1740381