2015-10-20 25 views
-1

請任何人都可以幫助我! 我有一個名爲:學生的模型類。 現在我需要用「StudentID」保存用戶。 「StudentID」將作爲外鍵保存在用戶表中。 這裏是我的學生類如何在mvc5中的AspNetUsers中添加外鍵引用

public class Student 
{ 
    public int ID { get; set; } 
    public string LastName { get; set; } 
    public string FirstMidName { get; set; } 
    public int? DepID { get; set; } 
    public DateTime EnrollmentDate { get; set; } 

    [ForeignKey("DepID")] 
    public virtual Department Department { get; set; } 
    public virtual ICollection<Enrollment> Enrollments { get; set; } 

} 

和我的身份模型

public class ApplicationUser : IdentityUser 
{ 

} 

public class ApplicationDbContext : IdentityDbContext<ApplicationUser> 
{ 
    public ApplicationDbContext() 
     : base("DefaultConnection") 
    { 

    } 
    public DbSet<Student> Students { get; set; } 

    public DbSet<Enrollment> Enrollments { get; set; } 
    public DbSet<Course> Courses { get; set; } 
    public DbSet<Department> Departments { get; set; } 
    public static ApplicationDbContext Create() 
    { 
     return new ApplicationDbContext(); 
    } 
} 

所以我怎樣才能添加「studentID」進入用戶表的外鍵。

回答

0

如果你只是想使用StudenID作爲外鍵在不同的表,你可以做這樣如

public class Employee 
    { 
    [Key] 
    public int EmployeeID { get; set; } 
    public string Name { get; set; } 

    public virtual EmployeeDetail EmployeeDetail { get; set; } 
    } 

public class EmployeeDetail 
    { 
    [Key] 
    [ForeignKey("Employee")] 
    public int EmployeeID { get; set; } 
    public string Adress { get; set; } 

    public virtual Employee Employee { get; set; } 
    } 

如果你正在談論由天冬氨酸創建的實際用戶表。 Net Identity,那麼你可以簡單地擴展和定製User表:

http://typecastexception.com/post/2014/06/22/ASPNET-Identity-20-Customizing-Users-and-Roles.aspx

+0

對不起!我想你沒有得到它。如何在用戶表中使用學生ID作爲外鍵? –

+0

通過「用戶表」你指的是Asp.Net身份表用戶? – ChrisRun

+0

對不起!我想使用StudenID作爲用戶表中的外鍵。 PLZ的幫助... –

相關問題