【问题标题】:C# Core 2.1 EF many-to-many relationship queryingC# Core 2.1 EF 多对多关系查询
【发布时间】:2019-03-14 15:38:18
【问题描述】:

我在 C# Core EF 中遇到了多对多连接表的问题。我正在将 .NET Framework 代码转换为 .NET Core 2.1 这有点棘手,因为必须在 dbcontext 中手动定义这些连接表,并附带一个模型类 而不是像以前那样在后台处理。

我不知道如何进行查询。急于加载。

例如,我以前只使用

包含相关数据
db.Student.Include(x => x.Course).Where(...

现在不行了,因为我只能加载连接表数据

db.Student.Include(x => x.CourseStudent).Where(...

如果我这样做

db.Student.Include(x => x.CourseStudent).ThenInclude(y => y.Course).Where(...

然后我得到错误:

The Include property lambda expression 'x => {from CourseStudent y in x.Course select [y].Course}' is invalid. The expression should represent a property access: 't => t.MyProperty'. To target navigations declared on derived types, specify an explicitly typed lambda parameter of the target type, E.g. '(Derived d) => d.MyProperty'. For more information on including related data, see http://go.microsoft.com/fwlink/?LinkID=746393.

我已经阅读了该页面,但那里没有任何有用的信息。它说 .ThenInclude() 应该可以工作,但它没有。

Student 派生自 Person,但所有重要的成员都在 Student 中。课程就是课程。

出了点问题,我不知道是什么......

【问题讨论】:

  • 您提供的代码不足以确定问题所在。您最好在此处输入您的数据库上下文以及您如何配置实体之间的关系
  • 你是对的。我会这样做的!
  • 在为您设计一个小型示例项目时,我偶然发现了解决方案。是 IntelliSense 错误欺骗了我,即使我已经阅读过它......所以问题解决了。我可以取消这个问题还是什么......

标签: c# many-to-many entity-framework-core


【解决方案1】:

在设计一个小型示例项目时,我偶然发现了这个解决方案。是 IntelliSense 错误欺骗了我,即使我已经阅读过它......所以问题解决了。

https://docs.microsoft.com/en-us/ef/core/querying/related-data#including-multiple-levels

【讨论】:

    猜你喜欢
    • 2019-01-19
    • 1970-01-01
    • 2021-10-21
    • 1970-01-01
    • 2017-11-13
    • 2021-10-11
    • 1970-01-01
    • 1970-01-01
    • 2018-06-02
    相关资源
    最近更新 更多