C# 如何在mvc5中的AspNetUsers中添加外键引用
谁能帮帮我! 我有一个名为“学生”的模型班。 现在我需要用“StudentID”保存一个用户。“StudentID”将作为外键保存在用户表中。 这是我的学生班C# 如何在mvc5中的AspNetUsers中添加外键引用,c#,C#,谁能帮帮我! 我有一个名为“学生”的模型班。 现在我需要用“StudentID”保存一个用户。“StudentID”将作为外键保存在用户表中。 这是我的学生班 public class Student { public int ID { get; set; } public string LastName { get; set; } public string FirstMidName { get; set; } public int? DepID { get;
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; }
}
公共班级学生
{
公共int ID{get;set;}
公共字符串LastName{get;set;}
公共字符串FirstMidName{get;set;}
公共int?DepID{get;set;}
公共日期时间注册日期{get;set;}
[外键(“DepID”)]
公共虚拟部门部门{get;set;}
公共虚拟ICollection注册{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();
}
}
公共类应用程序用户:IdentityUser
{
}
公共类ApplicationDbContext:IdentityDbContext
{
公共应用程序上下文()
:base(“默认连接”)
{
}
公共数据库集学生{get;set;}
公共数据库集注册{get;set;}
公共数据库集课程{get;set;}
公共数据库集部门{get;set;}
公共静态应用程序上下文创建()
{
返回新的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.Net Identity创建的实际用户表,那么您可以简单地扩展和自定义用户表:
对不起!我想你没有明白。如何使用用户表中的学生id作为外键?通过“用户表”,您是指Asp.Net标识表用户吗?如果您不详细说明,我将无法帮助您。