【问题标题】:Multiple tables in Entity Framework实体框架中的多个表
【发布时间】:2022-01-23 19:51:01
【问题描述】:

我有 3 张桌子:

学生

   StudentID -PK
   StudentName

测试

 TestID - PK
 StudentID-FK
 TestName

标记

 MarksID -PK
 TestID-FK
 Marks

我想获取特定学生的数据。我的查询将是

var result = db.Student
               .Include("Test")
               .Include("Marks")
               .FirstOrDefault(c => c.StudentID == '123');

当我只查询 StudentTest 时,结果是正确的。当我包含第三个表时,即Marks 我得到一个错误:

`Microsoft.EntityFrameworkCore.Query.InvalidIncludePathError':找不到在基于字符串的包含路径“标记”中指定的导航“标记”。通过将事件 ID 'CoreEventId.InvalidIncludePathError' 传递给 'DbContext.OnConfiguring' 或 'AddDbContext' 中的 'ConfigureWarnings' 方法,可以抑制或记录此异常。

我们将不胜感激。

谢谢

【问题讨论】:

  • 显示类,而不是抽象表。
  • 这就是为什么你应该总是使用Include的lambda版本。

标签: entity-framework-core


【解决方案1】:

由于MarksTest 的导航属性并且不是 Student,因此您需要使用ThenInclude("Marks") 而不是Include(),因为它基于@987654326 @ 而不是 Test

【讨论】:

  • 非常感谢。它奏效了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-17
  • 2010-11-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多