I have a database first asp.net mvc core project and I'm not sure how to work with foreign keys.
[Table("tbUser")]
public class User
{
[Key]
[Column("UserID")]
public int UserID { get; set; }
[Column("RoleID")]
public int RoleID { get; set; }
[ForeignKey("RoleID")]
public Role Role { get; set; }
}
[Table("tbRole")]
public class Role
{
[Key]
[Column("RoleID")]
public int RoleID { get; set; }
[Column("Role")]
public string RoleName { get; set; }
public ICollection<User> Users { get; set; }
}
All the properties load from my database but the Role Role in User stays null
CodePudding user response:
Try adding .Include(u => u.Role)
when loading from database. This works like SQL JOIN
.
https://www.entityframeworktutorial.net/efcore/querying-in-ef-core.aspx