How to auto include navigations in Entity Framework

Normally if we want to include certain navigation properties we have to explicitly do it on a query by query basis with Include() …

We can also tell Entity Framework to always include particular navigation properties globally using .AutoInclude()

This can be useful if you know that you will always need the data from the navigation property, even if you are not explicitly querying for it.

If we need to ignore AutoInclude() for a particular query with can use IgnoreAutoIncludes().

Disadvantages of using AutoInclude() in Entity Framework?

One downside of AutoInclude() over explicitly using Include() is that it doesn’t support filters like Include() …

If for example we wanted to only include posts from a particular blog, we could do that with Include but not AutoInclude(). Global query filters can be a workaround here though in a lot of cases.

Another potential disadvantage is that since navigations are auto included on a global level, it may not be clear to other developers that this is happening when they look at an individual query.

This implicitness is similar to global query filters which can also change a query without it being obvious. In both cases we need to make sure we communicate and document any global config which can amend queries implicitly.

Have you included navigations automatically before?

Leave a Reply

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