gpt4 book ai didi

c# - 然后在 Entity Framework 核心中包含显式加载?

转载 作者:行者123 更新时间:2023-12-03 15:27:44 25 4
gpt4 key购买 nike

我知道我可以更深入地使用 ThenInclude 加载相关数据在 Eager Loading 中,如下例所示

//Eager Loading
var publisher = await _context.Publishers
.Include(pub => pub.Books)
.ThenInclude(book => book.Sales)
.Include(pub => pub.Users)
.Where(pub => pub.PubId == id)
.FirstOrDefaultAsync();
如何在显式加载中编写相同的查询?如何加载 Sales 的数据在下面的情况下不循环阅读书籍?
//Explicit Loading
var publisher = await _context.Publishers
.SingleAsync(pub => pub.PubId == id);

_context.Entry(publisher)
.Collection(pub => pub.Books)
.Load();

_context.Entry(publisher)
.Collection(pub => pub.Users)
.Load();

最佳答案

Query()方法是你的 friend 。

Querying related entities 中有部分解释Explicit loading的小节文档:

You can also get a LINQ query that represents the contents of a navigation property.

This allows you to do things such as running an aggregate operator over the related entities without loading them into memory.

Example...

You can also filter which related entities are loaded into memory.

Example...



他们忘记提到的是,您也可以将它用于 Include/ ThenInclude显式加载数据的相关数据。

例如
_context.Entry(publisher)
.Collection(pub => pub.Books)
.Query() // <--
.Include(book => book.Sales) // <--
.Load();

关于c# - 然后在 Entity Framework 核心中包含显式加载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60382491/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com