System.InvalidOperationException: The expression 'r' is invalid inside an 'Include' operation, since it does not represent a property access

See original GitHub issue

In EF core 3.1, we were able to use .Include(r => r) to turn an IQueryable<TEntity> to An IIncludeableQueryable<TEntity,TEntity> to allow code resuse. ex. here, TestIncludeDataPointTemplate can be used on the base IQueryable, or on any include that gets to the same type.


public class QueryTests
{
    [Test]
    public void TestIncludeDefault()
    {
        var optionsBuilder = new DbContextOptionsBuilder<DefaultContext>();
        optionsBuilder.ConfigureMysql(TestingDbOptions.ConnectionString, false, true);
        var dbOptions = optionsBuilder.Options;
        var _context = new DefaultContext(dbOptions);
        var query = _context.AssetTemplateRevisions.TestIncludeDataPointTemplate(q => q.Include(r => r));
    }
}

public static class TestQueryExtensions
{
    public static IQueryable<TEntity> TestIncludeDataPointTemplate<TEntity, TProperty>(
        this IQueryable<TEntity> source,
        Func<IQueryable<TEntity>, IIncludableQueryable<TEntity, TProperty>> defaultInclude
    )
        where TEntity : class where TProperty : DataPointTemplate
    {
        return defaultInclude(source).ThenInclude(e => e.DataPointAssignments);
    }
}

But in .net 6, the same code ends in the following error:

System.InvalidOperationException: The expression 'r' is invalid inside an 'Include' operation, since it does not represent a property access: 't => t.MyProperty'. To target navigations declared on derived types, use casting ('t => (
(Derived)t).MyProperty') or the 'as' operator ('t => (t as Derived).MyProperty'). Collection navigation access can be filtered by composing Where, OrderBy(Descending), ThenBy(Descending), Skip or Take operations. For more informatio
n on including related data, see http://go.microsoft.com/fwlink/?LinkID=746393.

This completely breaks the ability to include navigational properties regardless if it is the base query or not. This unfortunately breaks code reuse and forces the developer to include by strings or duplicate their code.

EF Core version: 6.0.4 Database provider: Pomelo.EntityFrameworkCore.MySql Target framework: .NET 6.0 Operating system: Windows 10

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:3
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
smitpatelcommented, May 20, 2022

As @ajcvickers has mentioned above, use string based API if you are not sure of the source. If you want to use lambda based API then you can write a conditional code in C# to call Include or ThenInclude as appropriately.

The purpose and design of Include is to specify a navigation property to be included. The sole purpose of ThenInclude is to allow chaining further navigations on Include if the earlier navigation was collection. This API has been in use since 1.0 days and people haven’t been complaining about them being bad APIs. As I said earlier, the existing APIs are well-designed and allows developers to achieve their intended goals. Anything beyond that is matter of taste.

0reactions
smitpatelcommented, May 20, 2022

That is certainly looks more complicated than what I have in mind but then I am not sure what is the exact scenario you are trying to solve. Can you provider few queries without using anything additional operation and just basic Include on which you are trying to refactor so that you can reuse the Include? I can look into it to provide how to write a method to make it work,

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - The expression 'x.Taken' is invalid inside an 'Include' ...
The expression 'x.Taken' is invalid inside an 'Include' operation, since it does not represent a property access: 't => t.MyProperty'.
Read more >
The Expression is not valid in an include function
InvalidOperationException : The expression 'p.BranchName' is invalid inside an 'Include' operation, since it does not represent a property access ...
Read more >
The expression is invalid inside an 'Include' operation, since it ...
[Solved]-InvalidOperationException: The expression is invalid inside an 'Include' operation, since it does not represent a property-entityframework core.
Read more >
`EF.Property` is not working in `.Include`. Is it a bug or ...
InvalidOperationException : The expression 'Property(e, "_entityVersions")' is invalid inside // an 'Include' operation, since it does not ...
Read more >
[Solved]-Invalid inside an 'Include' operation, since it does not ...
Coding example for the question Invalid inside an 'Include' operation, since it does not represent a property access: 't => t.MyProperty'-Entity Framework.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found