简体   繁体   English

然后在实体框架核心中包含显式加载?

[英]ThenInclude for Explicit Loading in Entity Framework core?

I know I can go one step deeper to load related data using ThenInclude in Eager Loading like below example我知道我可以更深入地使用ThenInclude in 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();

How can I write the same query in Explicit Loading?如何在显式加载中编写相同的查询? How do I load data for Sales without looping through books in below case?在以下情况下,如何在不遍历书籍的情况下加载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() method is your friend. Query()方法是你的朋友。

It's partially explained in the Querying related entities subsection of Explicit loading documentation:它在显式加载文档的查询相关实体小节中进行了部分解释:

You can also get a LINQ query that represents the contents of a navigation property.您还可以获得表示导航属性内容的 LINQ 查询。

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...例子...

What they forgot to mention is that you can use it also for Include / ThenInclude related data of the explicitly loading data.他们忘记提到的是,您也可以将它用于显式加载数据的Include / ThenInclude相关数据。

eg例如

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

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

相关问题 .ThenInclude 用于 Entity Framework Core 2 中的子实体 - .ThenInclude for sub entity in Entity Framework Core 2 Entity Framework Core Include/ThenInclude 获取数据 - Entity Framework Core Include/ThenInclude to get the data 网络核心:实体框架然后包含在Projection Select中 - Net Core: Entity Framework ThenInclude with Projection Select Entity Framework Core 6 中的 Simplify.Include 和.ThenInclude 调用 - Simplify .Include and .ThenInclude calls in Entity Framework Core 6 Entity Framework Core 3.1 Load() 异步显式加载 - Entity Framework Core 3.1 Load() Async Explicit Loading 在实体框架中使用带有条件的 ThenInclude - Using ThenInclude with condition in Entity Framework 如何使用.net-core和Entity Framework Core和Identity从thenInclude中获取特定列? - How do i get specific columns from a thenInclude using .net-core and Entity Framework Core and Identity? 明确排除实体框架核心中的属性 - Explicit exclude of a property in entity framework core 在实体框架中显式加载嵌套的相关模型 - Explicit Loading nested related models in Entity Framework Entity Framework Core 如何在不使用 Include().ThenInclude() 的情况下从模型中从多对多列出一对多 - Entity Framework Core how to list one to many from many to many from a model without using Include().ThenInclude()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM