2015-10-20 52 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的帮助... –