Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ThenInclude not recognized in EF Core query

My IQueryable looks like this:

 IQueryable<TEntity> query = context.Set<TEntity>();
query = query.Include("Car").ThenInclude("Model");

'IQueryable' does not contain a definition for 'ThenInclude' and no extension method 'ThenInclude' accepting a first argument of type 'IQueryable' could be found (are you missing a using directive or an assembly reference?)

I have all references needed:

using Content.Data.Models;    
using Microsoft.EntityFrameworkCore;    
using System;    
using System.Collections.Generic;    
using System.Linq;    
using System.Linq.Expressions;    
using System.Threading.Tasks;   

Why it doesn't recognize ThenInclude?

Query:

[Content.Data.Models.Article]).Where(x => (((x.BaseContentItem.SiteId == value(Content.Business.Managers.ArticleManager+<>c__DisplayClass8_0).id) AndAlso x.BaseContentItem.IsActive) AndAlso x.BaseContentItem.IsLive)).Include("BaseContentItem").Include("BaseContentItem.TopicTag").Include("MainImage")}

Fails after I include .Include("BaseContentItem.TopicTag") part.

So I just read that with generic repository you lose ThenInclude. I am using thise generic rep:

public class ReadOnlyRepository<TContext> : IReadOnlyRepository
            where TContext : DbContext
    {
        protected readonly TContext context;

        public ReadOnlyRepository(TContext context)
        {
            this.context = context;
        }

        private IQueryable<TEntity> GetQueryable<TEntity>(
            Expression<Func<TEntity, bool>> filter = null,
            Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
            string includeProperties = null,
            int? skip = null,
            int? take = null)
            where TEntity : class, IEntity
        {
            includeProperties = includeProperties ?? string.Empty;
            IQueryable<TEntity> query = context.Set<TEntity>();

            if (filter != null)
            {
                query = query.Where(filter);
            }

            foreach (var includeProperty in includeProperties.Split
                (new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
            {
                query = query.Include(includeProperty);
            }

            if (orderBy != null)
            {
                query = orderBy(query);
            }

            if (skip.HasValue)
            {
                query = query.Skip(skip.Value);
            }

            if (take.HasValue)
            {
                query = query.Take(take.Value);
            }

            return query;
        }
like image 224
sensei Avatar asked Sep 01 '17 12:09

sensei


2 Answers

ThenInclude is available only when you use the Include overload with lambda expression parameter:

query = query.Include(e => e.Car).ThenInclude(e => e.Model);

When you use the Include overload with string argument, there is no need of ThenInclude since you can specify the whole property path in the passed string:

query = query.Include("Car.Model");
like image 105
Ivan Stoev Avatar answered Sep 20 '22 13:09

Ivan Stoev


Your problem might be a missing reference to Microsoft.EntityFrameworkCore.Relational?

This can be added through the package manager.

Also ensure you are using .include from the Microsoft.EntityFrameworkCore namespace and not the other .Include (which does not have a .ThenInclude)

like image 38
David Harris Avatar answered Sep 16 '22 13:09

David Harris