Entity Framework - Is there a way to automatically eager-load child entities without Include()?
Asked Answered
D

5

86

Is there a way to decorate your POCO classes to automatically eager-load child entities without having to use Include() every time you load them?

Say I have a Class Car, with Complex-typed Properties for Wheels, Doors, Engine, Bumper, Windows, Exhaust, etc. And in my app I need to load my car from my DbContext 20 different places with different queries, etc. I don't want to have to specify that I want to include all of the properties every time I want to load my car.

I want to say

List<Car> cars = db.Car
                .Where(x => c.Make == "Ford").ToList();

//NOT .Include(x => x.Wheels).Include(x => x.Doors).Include(x => x.Engine).Include(x => x.Bumper).Include(x => x.Windows)


foreach(Car car in cars)
{

//I don't want a null reference here.

String myString = car.**Bumper**.Title;
}

Can I somehow decorate my POCO class or in my OnModelCreating() or set a configuration in EF that will tell it to just load all the parts of my car when I load my car? I want to do this eagerly, so my understanding is that making my navigation properties virtual is out. I know NHibernate supports similar functionality.

Just wondering if I'm missing something. Thanks in advance!

Cheers,

Nathan

I like the solution below, but am wondering if I can nest the calls to the extension methods. For example, say I have a similar situation with Engine where it has many parts I don't want to include everywhere. Can I do something like this? (I've not found a way for this to work yet). This way if later I find out that Engine needs FuelInjectors, I can add it only in the BuildEngine and not have to also add it in BuildCar. Also if I can nest the calls, how can I nest a call to a collection? Like to call BuildWheel() for each of my wheels from within my BuildCar()?

public static IQueryable<Car> BuildCar(this IQueryable<Car> query) {
     return query.Include(x => x.Wheels).BuildWheel()
                 .Include(x => x.Doors)
                 .Include(x => x.Engine).BuildEngine()
                 .Include(x => x.Bumper)
                 .Include(x => x.Windows);
}

public static IQueryable<Engine> BuildEngine(this IQueryable<Engine> query) {
     return query.Include(x => x.Pistons)
                 .Include(x => x.Cylendars);
}

//Or to handle a collection e.g.
 public static IQueryable<Wheel> BuildWheel(this IQueryable<Wheel> query) {
     return query.Include(x => x.Rim)
                 .Include(x => x.Tire);
}

Here is another very similar thread in case it is helpful to anyone else in this situation, but it still doesn't handle being able to make nexted calls to the extension methods.

Entity framework linq query Include() multiple children entities

Danseur answered 24/1, 2013 at 22:49 Comment(4)
Nathan, very interesting challenge. Please explain why you want/need to avoid using active loading techniques like include().Obtuse
No you can't, but nothing stops you from creating a property like DbQuery<Car> CompleteCars { get { return ... (your long Include chain) in the DbContext or a repository class. If you could, wouldn't that cut off the possibility to fetch "bare" cars from the database?Interdental
I want to avoid this because I have a complex model, but very few records, so I want the data to load eagerly, but don't want to miss any includes of grandchildren anywhere in the app.Danseur
Related question/duplicate: #5001811Dannie
Y
70

No you cannot do that in mapping. Typical workaround is simple extension method:

public static IQueryable<Car> BuildCar(this IQueryable<Car> query) {
     return query.Include(x => x.Wheels)
                 .Include(x => x.Doors)
                 .Include(x => x.Engine)
                 .Include(x => x.Bumper)
                 .Include(x => x.Windows);
}

Now every time you want to query Car with all relations you will just do:

var query = from car in db.Cars.BuildCar()
            where car.Make == "Ford"
            select car;

Edit:

You cannot nest calls that way. Include works on the core entity you are working with - that entity defines shape of the query so after you call Include(x => Wheels) you are still working with IQueryable<Car> and you cannot call extension method for IQueryable<Engine>. You must again start with Car:

public static IQueryable<Car> BuildCarWheels(this IQuerable<Car> query) {
    // This also answers how to eager load nested collections 
    // Btw. only Select is supported - you cannot use Where, OrderBy or anything else
    return query.Include(x => x.Wheels.Select(y => y.Rim))
                .Include(x => x.Wheels.Select(y => y.Tire));
}

and you will use that method this way:

public static IQueryable<Car> BuildCar(this IQueryable<Car> query) {
     return query.BuildCarWheels()
                 .Include(x => x.Doors)
                 .Include(x => x.Engine)
                 .Include(x => x.Bumper)
                 .Include(x => x.Windows);
}

The usage does not call Include(x => x.Wheels) because it should be added automatically when you request eager loading of its nested entities.

Beware of complex queries produced by such complex eager loading structures. It may result in very poor performance and a lot of duplicate data transferred from the database.

Yeti answered 25/1, 2013 at 11:31 Comment(6)
Could I nest calls to these extension methods for child entities? (See updated question)Danseur
Having an [Include] attribute gets my vote! (see link in the answer)Danseur
So then this means I still need to write a separate method about loading rims and tires whenever I need to load busses, cars, trucks etc. I'll just include them in my LoadBus, LoadTruck, LoadCar methods, but I was trying to avoid having those methods need to know anything about the children of Tire. Too bad, but at least I have my answer. Thanks!Danseur
which is good for performance lazy loading or eager loading i can see from here its lazy loading, actually i am trying to search products which has relation to other tables, what you think ?Talos
Why do you say "you cannot use Where, OrderBy or anything else"? My experience is quite different (though, you may have to explicitly cast the lambda to Expression<Func<>>, which is implicitly convertible to IQueryable<>.Tabasco
@ErikE: It was meant for lambda in Include - not sure what is the situation now but when I wrote this answer you could not filter or order eager loaded collections.Yeti
A
21

Since EF Core 6 there is a AutoInclude method that configures whether a navigation should be included automatically.

This can be done in the OnModelCreation method in the DbContext class:

modelBuilder.Entity<Car>().Navigation(e => e.Wheels).AutoInclude();

This would load the Weels for every Car when running the following query.

var cars = dbContext.Cars.ToList();

See Model configuration for auto-including navigations

Adalie answered 8/12, 2022 at 8:39 Comment(2)
Thank you, this should be updated to be the new default answer.Johnajohnath
Yes, this one helped out ! Thanks buddy !Acierate
A
9

Had this same problem and saw the other link which mentioned an Include attribute. My solution assumes that you created an attribute called IncludeAttribute. With the following extension method and utility method:

    public static IQueryable<T> LoadRelated<T>(this IQueryable<T> originalQuery)
    {
        Func<IQueryable<T>, IQueryable<T>> includeFunc = f => f;
        foreach (var prop in typeof(T).GetProperties()
            .Where(p => Attribute.IsDefined(p, typeof(IncludeAttribute))))
        {
            Func<IQueryable<T>, IQueryable<T>> chainedIncludeFunc = f => f.Include(prop.Name);
            includeFunc = Compose(includeFunc, chainedIncludeFunc);
        }
        return includeFunc(originalQuery);
    }

    private static Func<T, T> Compose<T>(Func<T, T> innerFunc, Func<T, T> outerFunc)
    {
        return arg => outerFunc(innerFunc(arg));
    }
Audrieaudris answered 18/9, 2017 at 17:31 Comment(0)
I
1

Working further upon Lunyx's answer I added an overload to make it work with a collection of strings (where a string is a property name like "propertyA" , or "propertyA.propertyOfAA" for example):

  public static IQueryable<T> LoadRelated<T>(this IQueryable<T> originalQuery, ICollection<string> includes)
    {
        if (includes == null || !includes.Any()) return originalQuery;

        Func<IQueryable<T>, IQueryable<T>> includeFunc = f => f;
        foreach (var include in includes)
        {
            IQueryable<T> ChainedFunc(IQueryable<T> f) => f.Include(include);
            includeFunc = Compose(includeFunc, ChainedFunc);
        }

        return includeFunc(originalQuery);
    }
Inversely answered 8/7, 2020 at 12:12 Comment(0)
O
0

If you REALLY need to Include all the navigation properties (you could have performance issues) you can use this piece of code.
If you need to eagerly load also collections (even a worst idea) you can delete the continue statement in the code.
Context is your context (this if you insert this code in your context)

    public IQueryable<T> IncludeAllNavigationProperties<T>(IQueryable<T> queryable)
    {
        if (queryable == null)
            throw new ArgumentNullException("queryable");

        ObjectContext objectContext = ((IObjectContextAdapter)Context).ObjectContext;
        var metadataWorkspace = ((EntityConnection)objectContext.Connection).GetMetadataWorkspace();

        EntitySetMapping[] entitySetMappingCollection = metadataWorkspace.GetItems<EntityContainerMapping>(DataSpace.CSSpace).Single().EntitySetMappings.ToArray();

        var entitySetMappings = entitySetMappingCollection.First(o => o.EntityTypeMappings.Select(e => e.EntityType.Name).Contains(typeof(T).Name));

        var entityTypeMapping = entitySetMappings.EntityTypeMappings[0];

        foreach (var navigationProperty in entityTypeMapping.EntityType.NavigationProperties)
        {
            PropertyInfo propertyInfo = typeof(T).GetProperty(navigationProperty.Name);
            if (propertyInfo == null)
                throw new InvalidOperationException("propertyInfo == null");
            if (typeof(System.Collections.IEnumerable).IsAssignableFrom(propertyInfo.PropertyType))
                continue;

            queryable = queryable.Include(navigationProperty.Name);
        }

        return queryable;
    }
Ondrea answered 19/11, 2019 at 18:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.