简体   繁体   中英

Multiple Includes() in EF Core

I have an extension method that lets you generically include data in EF:

public static IQueryable<T> IncludeMultiple<T>(this IQueryable<T> query, params Expression<Func<T, object>>[] includes)
    where T : class
{
    if (includes != null)
    {
        query = includes.Aggregate(query, (current, include) => current.Include(include));
    }
    return query;
}

This allows me to have methods in my repository like this:

public Patient GetById(int id, params Expression<Func<Patient, object>>[] includes)
{
    return context.Patients
        .IncludeMultiple(includes)
        .FirstOrDefault(x => x.PatientId == id);
}

I believe the extension method worked before EF Core, but now including "children" is done like this:

var blogs = context.Blogs
    .Include(blog => blog.Posts)
        .ThenInclude(post => post.Author);

Is there a way to alter my generic extension method to support EF Core's new ThenInclude() practice?

As said in comments by other, you can take EF6 code to parse your expressions and apply the relevant Include / ThenInclude calls. It does not look that hard after all, but as this was not my idea, I would rather not put an answer with the code for it.

You may instead change your pattern for exposing some interface allowing you to specify your includes from the caller without letting it accessing the underlying queryable.

This would result in something like:

using YourProject.ExtensionNamespace;

// ...

patientRepository.GetById(0, ip => ip
    .Include(p => p.Addresses)
    .ThenInclude(a=> a.Country));

The using on namespace must match the namespace name containing the extension methods defined in the last code block.

GetById would be now:

public static Patient GetById(int id,
    Func<IIncludable<Patient>, IIncludable> includes)
{
    return context.Patients
        .IncludeMultiple(includes)
        .FirstOrDefault(x => x.EndDayID == id);
}

The extension method IncludeMultiple :

public static IQueryable<T> IncludeMultiple<T>(this IQueryable<T> query,
    Func<IIncludable<T>, IIncludable> includes)
    where T : class
{
    if (includes == null)
        return query;

    var includable = (Includable<T>)includes(new Includable<T>(query));
    return includable.Input;
}

Includable classes & interfaces, which are simple "placeholders" on which additional extensions methods will do the work of mimicking EF Include and ThenInclude methods:

public interface IIncludable { }

public interface IIncludable<out TEntity> : IIncludable { }

public interface IIncludable<out TEntity, out TProperty> : IIncludable<TEntity> { }

internal class Includable<TEntity> : IIncludable<TEntity> where TEntity : class
{
    internal IQueryable<TEntity> Input { get; }

    internal Includable(IQueryable<TEntity> queryable)
    {
        // C# 7 syntax, just rewrite it "old style" if you do not have Visual Studio 2017
        Input = queryable ?? throw new ArgumentNullException(nameof(queryable));
    }
}

internal class Includable<TEntity, TProperty> :
    Includable<TEntity>, IIncludable<TEntity, TProperty>
    where TEntity : class
{
    internal IIncludableQueryable<TEntity, TProperty> IncludableInput { get; }

    internal Includable(IIncludableQueryable<TEntity, TProperty> queryable) :
        base(queryable)
    {
        IncludableInput = queryable;
    }
}

IIncludable extension methods:

using Microsoft.EntityFrameworkCore;

// others using ommitted

namespace YourProject.ExtensionNamespace
{
    public static class IncludableExtensions
    {
        public static IIncludable<TEntity, TProperty> Include<TEntity, TProperty>(
            this IIncludable<TEntity> includes,
            Expression<Func<TEntity, TProperty>> propertySelector)
            where TEntity : class
        {
            var result = ((Includable<TEntity>)includes).Input
                .Include(propertySelector);
            return new Includable<TEntity, TProperty>(result);
        }

        public static IIncludable<TEntity, TOtherProperty>
            ThenInclude<TEntity, TOtherProperty, TProperty>(
                this IIncludable<TEntity, TProperty> includes,
                Expression<Func<TProperty, TOtherProperty>> propertySelector)
            where TEntity : class
        {
            var result = ((Includable<TEntity, TProperty>)includes)
                .IncludableInput.ThenInclude(propertySelector);
            return new Includable<TEntity, TOtherProperty>(result);
        }

        public static IIncludable<TEntity, TOtherProperty>
            ThenInclude<TEntity, TOtherProperty, TProperty>(
                this IIncludable<TEntity, IEnumerable<TProperty>> includes,
                Expression<Func<TProperty, TOtherProperty>> propertySelector)
            where TEntity : class
        {
            var result = ((Includable<TEntity, IEnumerable<TProperty>>)includes)
                .IncludableInput.ThenInclude(propertySelector);
            return new Includable<TEntity, TOtherProperty>(result);
        }
    }
}

IIncludable<TEntity, TProperty> is almost like IIncludableQueryable<TEntity, TProperty> from EF, but it does not extend IQueryable and does not allow reshaping the query.

Of course if the caller is in the same assembly, it can still cast the IIncludable to Includable and start fiddling with the queryable. But well, if someone wants to get it wrong, there is no way we would prevent him doing so (reflection allows anything). What does matter is the exposed contract.

Now if you do not care about exposing IQueryable to the caller (which I doubt), obviously just change your params argument for a Func<Queryable<T>, Queryable<T>> addIncludes argument, and avoid coding all those things above.

And the best for the end: I have not tested this, I do not use Entity Framework currently!

For posterity, another less eloquent, but simpler solution that makes use of the Include() overload that uses navigationPropertyPath :

public static class BlogIncludes
{
    public const string Posts = "Posts";
    public const string Author = "Posts.Author";
}

internal static class DataAccessExtensions
{
    internal static IQueryable<T> IncludeMultiple<T>(this IQueryable<T> query, 
        params string[] includes) where T : class
    {
        if (includes != null)
        {
            query = includes.Aggregate(query, (current, include) => current.Include(include));
        }
        return query;
    }
}

public Blog GetById(int ID, params string[] includes)
{
    var blog = context.Blogs
        .Where(x => x.BlogId == id)
        .IncludeMultiple(includes)
        .FirstOrDefault();
    return blog;
}

And the repository call is:

var blog = blogRepository.GetById(id, BlogIncludes.Posts, BlogIncludes.Author);

You can do something like this:

public Patient GetById(int id, Func<IQueryable<Patient>, IIncludableQueryable<Patient, object>> includes = null)
        {
            IQueryable<Patient> queryable = context.Patients;

            if (includes != null)
            {
                queryable = includes(queryable);
            }

            return  queryable.FirstOrDefault(x => x.PatientId == id);
        }

var patient = GetById(1, includes: source => source.Include(x => x.Relationship1).ThenInclude(x => x.Relationship2));

I made this method to do the dynamic includes. This way the "Select" command can be used in lambda to include just as it was in the past.

The call works like this:

repository.IncludeQuery(query, a => a.First.Second.Select(b => b.Third), a => a.Fourth);

private IQueryable<TCall> IncludeQuery<TCall>(
    params Expression<Func<TCall, object>>[] includeProperties) where TCall : class
{
    IQueryable<TCall> query;

    query = context.Set<TCall>();

    foreach (var property in includeProperties)
    {
        if (!(property.Body is MethodCallExpression))
            query = query.Include(property);
        else
        {
            var expression = property.Body as MethodCallExpression;

            var include = GenerateInclude(expression);

            query = query.Include(include);
        }
    } 

    return query;
}

private string GenerateInclude(MethodCallExpression expression)
{
    var result = default(string);

    foreach (var argument in expression.Arguments)
    {
        if (argument is MethodCallExpression)
            result += GenerateInclude(argument as MethodCallExpression) + ".";
        else if (argument is MemberExpression)
            result += ((MemberExpression)argument).Member.Name + ".";
        else if (argument is LambdaExpression)
            result += ((MemberExpression)(argument as LambdaExpression).Body).Member.Name + ".";
    }

    return result.TrimEnd('.');
} 

Ofcourse there is,

you could traverse the Expression tree of original params, and any nested includes, add them as

 .Include(entity => entity.NavigationProperty)
 .ThenInclude(navigationProperty.NestedNavigationProperty)

But its not trivial, but definitely very doable, please share if you do, as it can defintiely be reused!

public Task<List<TEntity>> GetAll()
    {
        var query = _Db.Set<TEntity>().AsQueryable();
        foreach (var property in _Db.Model.FindEntityType(typeof(TEntity)).GetNavigations())
            query = query.Include(property.Name);
        return query.ToListAsync();

    }

I adhere the simpler solution that makes use of the Include() overload that uses string navigationPropertyPath. The simplest that I can write is this extension method below.

using Microsoft.EntityFrameworkCore;
using System.Linq;

namespace MGame.Data.Helpers
{
    public static class IncludeBuilder
    {
        public static IQueryable<TSource> Include<TSource>(this IQueryable<TSource> queryable, params string[] navigations) where TSource : class
        {
            if (navigations == null || navigations.Length == 0) return queryable;

            return navigations.Aggregate(queryable, EntityFrameworkQueryableExtensions.Include);  // EntityFrameworkQueryableExtensions.Include method requires the constraint where TSource : class
        }
    }
}
    public TEntity GetByIdLoadFull(string id, List<string> navigatonProoperties)
    {
        if (id.isNullOrEmpty())
        {
            return null;
        }

        IQueryable<TEntity> query = dbSet;

        if (navigationProperties != null)
        {
            foreach (var navigationProperty in navigationProperties)
            {
                query = query.Include(navigationProperty.Name);
            }
        }

        return query.SingleOrDefault(x => x.Id == id);
    }

Here is a much simpler solution, idea is to cast the dbset to iqueryable and then recursively include properties

I handle it with like that;

I have Article entity. It includes ArticleCategory entity. And also ArticleCategory entity includes Category entity.

So: Article -> ArticleCategory -> Category

In My Generic Repository;

public virtual IQueryable<T> GetIncluded(params Func<IQueryable<T>, IIncludableQueryable<T, object>>[] include)
{
     IQueryable<T> query = Entities; // <- this equals = protected virtual DbSet<T> Entities => _entities ?? (_entities = _context.Set<T>());
     if (include is not null)
     {
          foreach (var i in include)
          {
              query = i(query);
          }
     }
     return query;
}

And I can use it like that;

var query = _articleReadRepository.GetIncluded(
               i => i.Include(s => s.ArticleCategories).ThenInclude(s => s.Category),
               i => i.Include(s => s.User)
            );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM