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

不打扰是莪最后的温柔 提交于 2020-06-30 03:56:57

问题


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


回答1:


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.




回答2:


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));
    }



回答3:


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;
    }


来源:https://stackoverflow.com/questions/14512285/entity-framework-is-there-a-way-to-automatically-eager-load-child-entities-wit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!