简体   繁体   English

C#Core 2.1 EF多对多关系查询

[英]C# Core 2.1 EF many-to-many relationship querying

I am having an issue with many-to-many join tables in C# Core EF. 我遇到了C#Core EF中多对多连接表的问题。 I'm converting .NET Framework code to .NET Core 2.1 and it's a bit trickier as those join tables must be manually defined in the dbcontext with an accompanying model class instead of being handled in the background as before. 我正在将.NET Framework代码转换为.NET Core 2.1,这有点棘手,因为这些联接表必须在dbcontext中使用随附的模型类手动定义,而不是像以前那样在后台进行处理。

I can't figure out how to do the querying. 我不知道如何进行查询。 Eager loading. 渴望加载。

For example, I used to just include the related data using 例如,我以前只是使用

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

Now that doesn't work anymore as I can only load the join-table data 现在这不再起作用,因为我只能加载连接表数据

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

If I do 如果我做

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

then I get the error: 然后我得到错误:

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.

I've read that page and can't anything useful there. 我已经阅读了该页面,并且那里没有任何用处。 It says .ThenInclude() should work but it doesn't. 它说.ThenInclude()应该可以工作,但事实并非如此。

Student is derived from Person but all the members that matter are in Student. 学生来自人,但所有重要的成员都在学生中。 Course is just Course. 课程就是课程。

Something's wrong and I don't know what.... 出问题了,我不知道...

When designing a small example project I stumbled upon the solution. 在设计一个小型示例项目时,我偶然发现了解决方案。 It was the IntelliSense bug that tricked me, even though I had read about it.... So problem solved. 即使我已阅读过有关IntelliSense的错误,也仍然欺骗了我。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM