如何在mvc5的AspNetUsers中添加外键引用
本文关键字:添加 引用 AspNetUsers mvc5 | 更新日期: 2023-09-27 18:18:22
谁来帮帮我!我有一个名为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();
}
}
那么我如何添加"studentID"到用户表作为外键
如果您只是想使用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. xml创建的实际User表。Net Identity,那么您可以简单地扩展和定制User表:
http://typecastexception.com/post/2014/06/22/ASPNET-Identity-20-Customizing-Users-and-Roles.aspx