简体   繁体   English

如何在 mvc5 中的 AspNetUsers 中添加外键引用

[英]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.

相关问题 如何在引用表中插入外键? - How to insert the foreign key in the Referance table? MVC5数据库中已经有一个名为“ AspNetUsers”的对象 - MVC5 There is already an object named 'AspNetUsers' in the database Identity 3.0 - 如何在分层应用程序中使用 AspNetUsers ID 作为外键 - Identity 3.0 - How to use AspNetUsers ID as Foreign Key in a Tiered Application 如何将模型中的外键值显示到 mvc5 中的级联下拉列表? - How to display foreign key value from a model to a cascaded dropdown list in mvc5? ASP MVC5 - 身份。 如何获取当前 ApplicationUser 并使用此用户查询以用户为外键的表 - ASP MVC5 - Identity. How to get current ApplicationUser and use this user to query a table with user as foreign key 使用linq在mvc5中通过外键表进行分组表 - group table by foreign key table in mvc5 using linq dbo.AspNetUsers 表上的 ASP.NET MVC 5 核心标识 CompanyID 外键 - ASP.NET MVC 5 Core Identity CompanyID foreign key on dbo.AspNetUsers table 身份新表外键AspNetUsers - Identity new table foreign key to AspNetUsers AspNetUsers的主键作为另一个表的外来 - Primary key of AspNetUsers as foreign of another table 我想在另一个表中添加 AspNetUsers 表“Id”列作为外键 - I want to add AspNetUsers table "Id" column as Foreign Key in another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM