需要一种在 Entity Framework Core 中使用 C# Linq 从第二个 table 检索数据的方法

Need a Way to retrieve data from second table using C# Linq in Entity Framework Core

我无法 return 第二个 table 链接的子数据与第一个 table 购买外键。

我有以下使用 Entity Framework 核心代码优先方法创建的表:

 public class BoardGame
 {
    public int id { get; set; }
    public double price { get; set; }
    public List<BoardGameTitle> BoardGameTitle{get;set;}
 }

 public class BoardGameTitle
 {
     public int id { get; set; }
     public string Name { get; set; }
     public string Description { get; set; }

     public int languageId { get; set; }

     public int BoardGameId{get;set;}
     public BoardGame BoardGame{get;set;}
  }

 public class TableFlipDB :DbContext{
        public TableFlipDB (DbContextOptions<TableFlipDB> options)
        :base(options)
        {}

        public DbSet<BoardGame> BoardGame{get;set;}
        public DbSet<BoardGameTitle> BoardGameTitle{get;set;}

        protected override void OnModelCreating(ModelBuilder modelBuilder){
            modelBuilder.Entity<BoardGame>().HasMany(bg=>bg.BoardGameTitle).WithOne(bg=>bg.BoardGame);
            //modelBuilder.Entity<BoardGameTitle>().HasOne(BoardGameTitle=>BoardGameTitle.BoardGame).WithMany(BoardGameTitle=>BoardGameTitle.BoardGameTitle);
        }
    }

第二个tableBoardGameTitle包含一个外键到下面的tableBoardGame通过属性BoardGameId,

每当我检索棋盘游戏列表时,我无法从 属性 BoardGameTitle 属性 [=37= 中的第二个 table 获取相关数据]s null

我的控制器

 public ActionResult<List<BoardGame>> GetAll()
 {
   return TableFlipDB.BoardGame.ToList();
 }

预期结果是:

[
  {
    "id": 1,
    "price": 100,
    "boardGameTitle": [
      {
        "id": 1,
        "name": "b1",
        "description": "b1",
        "languageId": 1,
        "boardGameId": 1,
        "boardGame": {
          "id": 1,
          "price": 100,
          "boardGameTitle": []
        }
      }
    ]
  },
  {
    "id": 2,
    "price": 200,
    "boardGameTitle": [
      {
        "id": 2,
        "name": "b2",
        "description": "b2",
        "languageId": 1,
        "boardGameId": 2,
        "boardGame": {
          "id": 2,
          "price": 200,
          "boardGameTitle": [
            {
              "id": 3,
              "name": "b22",
              "description": "b22",
              "languageId": 2,
              "boardGameId": 2
            }
          ]
        }
      },
      {
        "id": 3,
        "name": "b22",
        "description": "b22",
        "languageId": 2,
        "boardGameId": 2,
        "boardGame": {
          "id": 2,
          "price": 200,
          "boardGameTitle": [
            {
              "id": 2,
              "name": "b2",
              "description": "b2",
              "languageId": 1,
              "boardGameId": 2
            }
          ]
        }
      }
    ]
  }
]

但是我得到的结果

[ { "id": 1, "price": 100, "boardGameTitle": null }, { "id": 2, "price": 200, "boardGameTitle": null } ]

我找到了解决方案,@Elyas Esna 在评论部分说我需要将控制器 return 语句从 return TableFlipDB.BoardGame.ToList(); 更改为 return TableFlipDB.BoardGame.Include(p=>p.BoardGameTitle).ToList();