question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

This works with the InMemory Provider

See original GitHub issue

We are working on a rather large system in ASP.NET Core with EF Core and Angular.

In my extensive unit tests I use the InMemory Provider and everything runs smoothly.

It is a relatively simple query which works fine with the InMemory Provider, but when I execute it with the SqlServer Provider it throws an exception.

public IQueryable<FinancialStatementLedgerAccountLine> LoadAllWithinDateInterval(DateTime fromDate, DateTime toDate)
        {
            var sumPostings = from la in Context.LedgerAccounts
                              from pt in la.PostingTransactions
                              where pt.PostingDate >= fromDate && pt.PostingDate <= toDate
                              group pt by pt.LedgerAccountId into postingSumGroup
                              select new { LedgerAccountId = postingSumGroup.Key, DebitAmount = postingSumGroup.Sum(p => p.DebitAmount), CreditAmount = postingSumGroup.Sum(p => p.CreditAmount) };

            var sumOpeningBalance = from la in Context.LedgerAccounts
                                    from pt in la.PostingTransactions
                                    where (pt.PostingDate < fromDate && la.AccountType == Shared.Enums.LedgerAccountTypes.Status) || (pt.PostingDate >= new DateTime(fromDate.Year, 1, 1) && pt.PostingDate < fromDate && la.AccountType == Shared.Enums.LedgerAccountTypes.Status)
                                    group pt by pt.LedgerAccountId into postingSumGroup
                                    select new { LedgerAccountId = postingSumGroup.Key, OpeningBalance = postingSumGroup.Sum(p => p.DebitAmount) - postingSumGroup.Sum(p => p.CreditAmount) };

            var countNumberOfPostings = from la in Context.LedgerAccounts
                                        from pt in la.PostingTransactions
                                        where pt.PostingDate >= fromDate && pt.PostingDate <= toDate
                                        group pt by pt.LedgerAccountId into countSum
                                        select new { Id = countSum.Key, NumberOfPostings = countSum.Count() };

            var q = from la in Context.LedgerAccounts

                    join sp in sumPostings on la.Id equals sp.LedgerAccountId into postings
                    from sp in postings.DefaultIfEmpty()

                    join sob in sumOpeningBalance on la.Id equals sob.LedgerAccountId into openingBalance
                    from sob in openingBalance.DefaultIfEmpty()

                    join nop in countNumberOfPostings on la.Id equals nop.Id into numberOfPostings
                    from nop in numberOfPostings.DefaultIfEmpty()

                    orderby la.AccountNumber
                    select new FinancialStatementLedgerAccountLine()
                    {
                        Id = la.Id,
                        AccountNumber = la.AccountNumber,
                        AccountName = la.AccountName,
                        AccountType = la.AccountType,
                        AddedUpFromAccountNumber = la.AddedUpFromThisLedgerAccount != null ? la.AddedUpFromThisLedgerAccount.AccountNumber : 0,
                        NumberOfPostingTransactions = nop != null ? nop.NumberOfPostings : 0,
                        AmountDebit = sp != null ? sp.DebitAmount : 0,
                        AmountCredit = sp != null ? sp.CreditAmount : 0,
                        OpeningBalance = sob != null ? sob.OpeningBalance : 0,
                    };

            return q.AsQueryable();
        }

Exception message: ArgumentException: must be reducible node

Stack trace:

System.Linq.Expressions.Expression.ReduceAndCheck()
System.Linq.Expressions.Expression.ReduceExtensions()
System.Linq.Expressions.Compiler.StackSpiller.RewriteExtensionExpression(Expression expr, Stack stack)
System.Linq.Expressions.Compiler.StackSpiller.RewriteExpression(Expression node, Stack stack)
System.Linq.Expressions.Compiler.StackSpiller.RewriteMemberExpression(Expression expr, Stack stack)
System.Linq.Expressions.Compiler.StackSpiller.RewriteExpression(Expression node, Stack stack)
System.Linq.Expressions.Compiler.StackSpiller.RewriteExpressionFreeTemps(Expression expression, Stack stack)
System.Linq.Expressions.Compiler.StackSpiller.Rewrite<T>(Expression<T> lambda)
System.Linq.Expressions.Expression<TDelegate>.Accept(StackSpiller spiller)
System.Linq.Expressions.Compiler.StackSpiller.RewriteLambdaExpression(Expression expr)
System.Linq.Expressions.Compiler.StackSpiller.RewriteExpression(Expression node, Stack stack)
System.Linq.Expressions.Compiler.StackSpiller+ChildRewriter.Add(Expression expression)
System.Linq.Expressions.Compiler.StackSpiller+ChildRewriter.AddArguments(IArgumentProvider expressions)
System.Linq.Expressions.Compiler.StackSpiller.RewriteMethodCallExpression(Expression expr, Stack stack)
System.Linq.Expressions.Compiler.StackSpiller.RewriteExpression(Expression node, Stack stack)
System.Linq.Expressions.Compiler.StackSpiller+ChildRewriter.Add(Expression expression)
System.Linq.Expressions.Compiler.StackSpiller+ChildRewriter.AddArguments(IArgumentProvider expressions)
System.Linq.Expressions.Compiler.StackSpiller.RewriteMethodCallExpression(Expression expr, Stack stack)
System.Linq.Expressions.Compiler.StackSpiller.RewriteExpression(Expression node, Stack stack)
System.Linq.Expressions.Compiler.StackSpiller+ChildRewriter.Add(Expression expression)
System.Linq.Expressions.Compiler.StackSpiller.RewriteBinaryExpression(Expression expr, Stack stack)
System.Linq.Expressions.Compiler.StackSpiller.RewriteExpression(Expression node, Stack stack)
System.Linq.Expressions.Compiler.StackSpiller+ChildRewriter.Add(Expression expression)
System.Linq.Expressions.Compiler.StackSpiller+ChildRewriter.AddArguments(IArgumentProvider expressions)
System.Linq.Expressions.Compiler.StackSpiller.RewriteNewExpression(Expression expr, Stack stack)
System.Linq.Expressions.Compiler.StackSpiller.RewriteExpression(Expression node, Stack stack)
System.Linq.Expressions.Compiler.StackSpiller.RewriteExpressionFreeTemps(Expression expression, Stack stack)
System.Linq.Expressions.Compiler.StackSpiller.Rewrite<T>(Expression<T> lambda)
System.Linq.Expressions.Expression<TDelegate>.Accept(StackSpiller spiller)
System.Linq.Expressions.Compiler.StackSpiller.RewriteLambdaExpression(Expression expr)
System.Linq.Expressions.Compiler.StackSpiller.RewriteExpression(Expression node, Stack stack)
System.Linq.Expressions.Compiler.StackSpiller+ChildRewriter.Add(Expression expression)
System.Linq.Expressions.Compiler.StackSpiller+ChildRewriter.AddArguments(IArgumentProvider expressions)
System.Linq.Expressions.Compiler.StackSpiller.RewriteMethodCallExpression(Expression expr, Stack stack)
System.Linq.Expressions.Compiler.StackSpiller.RewriteExpression(Expression node, Stack stack)
System.Linq.Expressions.Compiler.StackSpiller+ChildRewriter.Add(Expression expression)
System.Linq.Expressions.Compiler.StackSpiller+ChildRewriter.AddArguments(IArgumentProvider expressions)
System.Linq.Expressions.Compiler.StackSpiller.RewriteMethodCallExpression(Expression expr, Stack stack)
System.Linq.Expressions.Compiler.StackSpiller.RewriteExpression(Expression node, Stack stack)
System.Linq.Expressions.Compiler.StackSpiller+ChildRewriter.Add(Expression expression)
System.Linq.Expressions.Compiler.StackSpiller+ChildRewriter.AddArguments(IArgumentProvider expressions)
System.Linq.Expressions.Compiler.StackSpiller.RewriteMethodCallExpression(Expression expr, Stack stack)
System.Linq.Expressions.Compiler.StackSpiller.RewriteExpression(Expression node, Stack stack)
System.Linq.Expressions.Compiler.StackSpiller+ChildRewriter.Add(Expression expression)
System.Linq.Expressions.Compiler.StackSpiller+ChildRewriter.AddArguments(IArgumentProvider expressions)
System.Linq.Expressions.Compiler.StackSpiller.RewriteMethodCallExpression(Expression expr, Stack stack)
System.Linq.Expressions.Compiler.StackSpiller.RewriteExpression(Expression node, Stack stack)
System.Linq.Expressions.Compiler.StackSpiller+ChildRewriter.Add(Expression expression)
System.Linq.Expressions.Compiler.StackSpiller+ChildRewriter.AddArguments(IArgumentProvider expressions)
System.Linq.Expressions.Compiler.StackSpiller.RewriteMethodCallExpression(Expression expr, Stack stack)
System.Linq.Expressions.Compiler.StackSpiller.RewriteExpression(Expression node, Stack stack)
System.Linq.Expressions.Compiler.StackSpiller+ChildRewriter.Add(Expression expression)
System.Linq.Expressions.Compiler.StackSpiller+ChildRewriter.AddArguments(IArgumentProvider expressions)
System.Linq.Expressions.Compiler.StackSpiller.RewriteMethodCallExpression(Expression expr, Stack stack)
System.Linq.Expressions.Compiler.StackSpiller.RewriteExpression(Expression node, Stack stack)
System.Linq.Expressions.Compiler.StackSpiller+ChildRewriter.Add(Expression expression)
System.Linq.Expressions.Compiler.StackSpiller+ChildRewriter.AddArguments(IArgumentProvider expressions)
System.Linq.Expressions.Compiler.StackSpiller.RewriteMethodCallExpression(Expression expr, Stack stack)
System.Linq.Expressions.Compiler.StackSpiller.RewriteExpression(Expression node, Stack stack)
System.Linq.Expressions.Compiler.StackSpiller.RewriteExpressionFreeTemps(Expression expression, Stack stack)
System.Linq.Expressions.Compiler.StackSpiller.Rewrite<T>(Expression<T> lambda)
System.Linq.Expressions.Expression<TDelegate>.Accept(StackSpiller spiller)
System.Linq.Expressions.Compiler.LambdaCompiler.Compile(LambdaExpression lambda)
System.Linq.Expressions.Expression<TDelegate>.Compile(bool preferInterpretation)
System.Linq.Expressions.Expression<TDelegate>.Compile()
Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.CreateExecutorLambda<TResults>()
Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor.CreateExecutorLambda<TResults>()
Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.CreateQueryExecutor<TResult>(QueryModel queryModel)
Microsoft.EntityFrameworkCore.Storage.Database.CompileQuery<TResult>(QueryModel queryModel)
Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore<TResult>(Expression query, IQueryModelGenerator queryModelGenerator, IDatabase database, IDiagnosticsLogger<Query> logger, Type contextType)
Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler+<>c__DisplayClass13_0<TResult>.<Execute>b__0()
Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQueryCore<TFunc>(object cacheKey, Func<Func<QueryContext, TFunc>> compiler)
Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery<TResult>(object cacheKey, Func<Func<QueryContext, TResult>> compiler)
Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute<TResult>(Expression query)
Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute<TResult>(Expression expression)
Remotion.Linq.QueryableBase<T>.GetEnumerator()
System.Collections.Generic.List<T>.AddEnumerable(IEnumerable<T> enumerable)
System.Linq.Enumerable.ToList<TSource>(IEnumerable<TSource> source)
Komit.Services.Accounting.GeneralLedger.FinancialStatement.FinancialStatementService.Calculate(DateTime fromDate, DateTime toDate, int accountNumberRevenueInOperations, int accountNumberRevenueInStatus) in FinancialStatementService.cs
+
            var lines = q.ToList();
Komit.Web.Areas.Accounting.Controllers.FinancialStatementController.SimpleReport(FinancialStatementService service) in FinancialStatementController.cs
+
            var x = new FinancialStatementSimpleReportDto("Saldobalance", service.Calculate(new System.DateTime(2018, 01, 01), new System.DateTime(2018, 12, 31), 9995, 20900), service.Filter);
Microsoft.AspNetCore.Mvc.Internal.ActionMethodExecutor+TaskOfIActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, object controller, object[] arguments)
System.Threading.Tasks.ValueTask<TResult>.get_Result()
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeActionMethodAsync()
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeNextActionFilterAsync()
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext context)
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync()
Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext)
Komit.Web.Extensions.Middleware.AuthorizationMiddleware.Invoke(HttpContext context) in AuthorizationMiddleware.cs
+
            await Next.Invoke(context);
Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.MigrationsEndPointMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.DatabaseErrorPageMiddleware.Invoke(HttpContext httpContext)
Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.DatabaseErrorPageMiddleware.Invoke(HttpContext httpContext)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

Steps to reproduce

Further technical details

EF Core version: 2.1.2 Database Provider: Microsoft.EntityFrameworkCore.SqlServer

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
KlausEvenEnevoldsencommented, Aug 21, 2018

@ajcvickers Here you go…

Attached is a solution which isolates the problem, complete with a few unit tests which works with the InMemory Provider.

There is a small .bacpac-file containing the database and some demo-data (DemoDb.bacpac).

Let me know if you need anything else from me…

ConsoleApp1.zip

1reaction
ajcvickerscommented, Sep 6, 2018

@KlausEvenEnevoldsen This issue has been triaged as a bug into the 3.0 milestone. Thanks for providing the additional info.

Read more comments on GitHub >

github_iconTop Results From Across the Web

EF Core In-Memory Database Provider
This database provider allows Entity Framework Core to be used with an in-memory database. While some users use the in-memory database for ...
Read more >
Testing with the InMemory provider in Entity Framework
In this article we'll examine how we can work with the InMemory provider in Entity Framework Core to write and execute unit tests...
Read more >
Entity Framework Extensions EF Core - InMemory Provider
InMemory is designed to be a general-purpose database for testing and is not designed to mimic a relational database. InMemory will allow you...
Read more >
Is there an in-memory provider for Entity Framework?
An InMemory provider is included in EF7 (pre-release). You can use either the NuGet package, or read about it in the EF repo...
Read more >
Using Entity Framework Core's InMemory provider
I am using .NET Web API to demonstrate this. Create an empty .NET Core Web API project. · Install Microsoft.EntityFrameworkCore.InMemory nuget package manager ......
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