[英]how to add a foreign key referance in AspNetUsers in mvc5
Please can anyone help me!请任何人都可以帮助我! I have a Model class named: Student.我有一个名为:Student 的 Model 类。 Now i need to save a user with "StudentID".现在我需要用“StudentID”保存一个用户。 "StudentID" will be saved in user table as foreign key. “StudentID”将作为外键保存在用户表中。 here is my Student class这是我的学生课
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; }
}
and my identity model is我的身份模型是
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();
}
}
so how can i add "studentID" into user table as foreign key.那么如何将“studentID”作为外键添加到用户表中。
In case you just want to use the StudenID as a foreign key in a different table, you can do it like this eg如果您只想将 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; }
}
In case you are talking about the actual User table created by Asp.Net Identity, then you can simply extend and customize the User table:如果您谈论的是由 Asp.Net Identity 创建的实际 User 表,那么您可以简单地扩展和自定义 User 表:
http://typecastexception.com/post/2014/06/22/ASPNET-Identity-20-Customizing-Users-and-Roles.aspx http://typecastexception.com/post/2014/06/22/ASPNET-Identity-20-Customizing-Users-and-Roles.aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.