0

I am trying to make my code a little bit cleaner and easier to read but I am getting an efcore exception (they indicate this might be a limitation), just wanted to see if anyone can help.

I am using .net3.1 and efcore also 3.1.

Original code working:

    public Response GetAll(long myId, DataSourceLoadOptions loadOptions)
    {
        var dbContext = ContextFactory.GetContext<IMyContext>();
        var query = dbContext.Table1.Where(_ => _.Id == myId).Select(ent1 => new
        {
            Id = ent1.Id,
            State = ent1.State,
            MatCode = ent1.Mat.Code,
            AmountToFulfill = ent1.AmountRequested,
            AmountReserved = dbContext.Table2.Where(matres => matres.Ent1Id == ent1.Id && matres.State == MaterialReservationState.Active).Select(_ => _.AmountReserved).Sum(),
            LastChangeTime = ent1.ChangeTime,
        });

        return query.ToList();
    }

Not working code:

    public Response GetAll2(long myId, DataSourceLoadOptions loadOptions)
    {
        var dbContext = ContextFactory.GetContext<IMyContext>();
        var query = dbContext.Table1.Where(_ => _.Id == myId).Select(ent1 => new
        {
            Id = ent1.Id,
            State = ent1.State,
            MatCode = ent1.Mat.Code,
            AmountToFulfill = ent1.AmountRequested,
            AmountReserved = GetReservedAmount(dbContext, ent1).Sum(),
            LastChangeTime = ent1.ChangeTime,
        });

        return query.ToList();
    }

    private static IQueryable<int> GetReservedAmount(IMyContext dbContext, IEntity1 ent1)
    {
        return dbContext.Table2
            .Where(matres => matres.Ent1Id == ent1.Id && matres.State == MaterialReservationState.Active)
            .Select(_ => _.AmountReserved);
    }

Exception:

[21:26:28.1605 ERR] An unhandled exception has occurred while executing the request. {EventId={Id=1, Name="UnhandledException"}, SourceContext="Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware", RequestId="0HxxxxxA83M7JB:00000001", RequestPath="/xxxxxxxxxx/2", SpanId="|5exxx39-48248xxxxx5b806.1.eee74277_", TraceId="5e3cde39-4824xxxxx", ParentId="|5e3cxxde39-48248xxxxxa25b806.1.", ConnectionId="0xxxxxx", ThreadId=11} System.InvalidOperationException: Processing of the LINQ expression 'ClassName.GetAll2( dbContext: __dbContext_1, op: (NavigationTreeExpression Value: (EntityReference: Entity1) Expression: o.Outer)) .Sum()' by 'NavigationExpandingExpressionVisitor' failed. This may indicate either a bug or a limitation in EF Core. See https://go.microsoft.com/fwlink/?linkid=2101433 for more detailed information. at Microsoft.EntityFrameworkCore.Query.Internal.NavigationExpandingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression) .......

1 Answer 1

1

The error you've got give you complete explanation what's wrong there:

Processing of the LINQ expression 'ClassName.GetAll2( dbContext: __dbContext_1, op: (NavigationTreeExpression Value: (EntityReference: Entity1) Expression: o.Outer)) .Sum()' by 'NavigationExpandingExpressionVisitor' failed.

If you go to provided link (https://go.microsoft.com/fwlink/?linkid=2101433) then you can read that:

EF Core supports partial client evaluation in the top-level projection (essentially, the last call to Select()). If the top-level projection in the query can't be translated to the server, EF Core will fetch any required data from the server and evaluate remaining parts of the query on the client. If EF Core detects an expression, in any place other than the top-level projection, which can't be translated to the server, then it throws a runtime exception.

Your method

private static IQueryable<int> GetReservedAmount(IMyContext dbContext, IEntity1 ent1)
{
    return dbContext.Table2
        .Where(matres => matres.Ent1Id == ent1.Id && matres.State == MaterialReservationState.Active)
        .Select(_ => _.AmountReserved);
}

can't be translated to TSQL query syntax.

That's it, from EF Core version 3.0 client methods can't be translated to TSQL.

1
  • any solution to not clutter the code with many subqueries that you know of?
    – mfabruno
    Commented Apr 13, 2022 at 9:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.