如何在mvc5中的AspNetUsers中添加外键referance

时间:2015-10-20 06:08:53

标签: c#

请任何人帮助我! 我有一个名为Student的Model类。 现在我需要使用" StudentID"来保存用户。 " StudentID"将作为外键保存在用户表中。 这是我的学生班

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

所以如何添加&#34; studentID&#34;作为外键进入用户表。

1 个答案:

答案 0 :(得分:-1)

如果你只是想在另一个表中使用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; }
  }

如果您正在讨论由Asp.Net Identity创建的实际用户表,那么您只需扩展和自定义用户表:

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