Back to Entityframework

Explicit Loading of Related Data - EF Core

entity-framework/core/querying/related-data/explicit.md

latest1.2 KB
Original Source

Explicit Loading of Related Data

Explicit loading

You can explicitly load a navigation property via the DbContext.Entry(...) API.

[!code-csharpMain]

You can also explicitly load a navigation property by executing a separate query that returns the related entities. If change tracking is enabled, then when a query materializes an entity, EF Core will automatically set the navigation properties of the newly-loaded entity to refer to any entities already loaded, and set the navigation properties of the already-loaded entities to refer to the newly loaded entity.

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

This allows you to apply other operators over the query. For example, applying an aggregate operator over the related entities without loading them into memory.

[!code-csharpMain]

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

[!code-csharpMain]