[英]ASP.NET MVC Core foreign key
我首先有一個數據庫 asp.net mvc 核心項目,但我不確定如何使用外鍵。
[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; }
}
所有屬性都從我的數據庫加載,但用戶中的角色角色保持 null
從數據庫加載時嘗試添加.Include(u => u.Role)
。 這類似於 SQL JOIN
。
https://www.entityframeworktutorial.net/efcore/querying-in-ef-core.aspx
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.