Entity Framework – Why can’t I see the child entities of my entity

So currently I’m working on an entity model in which there are a lot of parent – child relationships and for obvious reasons the entity framework is a great choice to build that entity model. However, I’ve ran into the issue that although in the database several of this relationships already exist, when trying to access the child entities via the property at the parent level I get an empty list. After doing some research I found out that this is due to the fact that there are several loading mechanisms that are available as part of the Entity Framework.

In order to resolve this I used therefore the forced loading command: .Load() at the child collection entity. Please remember that you’ll need your context to be active in order to perform this action. If you have lost your context just create a new one, find the entity you currently have through a query using your new context and then use the .Load() method on the child collection.

For more information on the loading mechanisms please take a look at the table below obtained from the referenced article:

Referenced Articles:


Loading pattern Description
Specified in the query You can compose an Entity SQL or LINQ to Entities query that explicitly navigates relationships by using navigation properties. When you execute such a query, the related entities that are included as navigation properties in the outmost projection of the query are returned. For more information, seeHow to: Navigate Relationships Using Navigation Properties.
Explicit loading Explicitly loading entities into the ObjectContext requires multiple round-trips to the database and might require multiple active result sets, but the amount of data returned is limited to only the entities being loaded. Use the Load method on an EntityCollection or EntityReferenceor the LoadProperty method on the ObjectContext to explicitly retrieve the related entities from the data source. Each call to the Loadmethod opens a connection to the database to retrieve the related information. This ensures that a query is never executed without an explicit request for the related entity. Explicit loading is the default behavior of the Entity Framework .

Before Load is called, a small amount of information about the related entity is already loaded into the ObjectContext.

For more information, see the Explicitly Loading Related Objects section of this topic.

Lazy loading In this type of loading, related entities are automatically loaded from the data source when you access a navigation property. With this type of loading, be aware that each navigation property that you access results in a separate query executing against the data source if the entity is not already in the ObjectContext.For more information, see the Lazy Loading section of this topic.
Eager loadingor

Defining Query Paths with Include

When you know the exact shape of the graph of related entities that your application requires, you can use the Include method on theObjectQueryto define a query path that controls which related entities to return as part of the initial query. When you define a query path, only a single request against the database is required to return all entities defined by the path in a single result set, and all related entities of the type specified in the path are loaded with each object that the query returns.For more information, see the Defining a Query Path to Shape Query Results section of this topic.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.