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.

InMemory database throws exception in .NET5

See original GitHub issue

Upgrading to .NET5 here and there seems to be an exception that I did not encounter with .NETCore3.1:

System.ArgumentException
must be reducible node
   at System.Linq.Expressions.Expression.ReduceAndCheck()
   at System.Linq.Expressions.Expression.ReduceExtensions()
   at System.Linq.Expressions.Compiler.StackSpiller.RewriteExtensionExpression(Expression expr, Stack stack)
   at System.Linq.Expressions.Compiler.StackSpiller.RewriteExpression(Expression node, Stack stack)
   at System.Linq.Expressions.Compiler.StackSpiller.ChildRewriter.Add(Expression expression)
   at System.Linq.Expressions.Compiler.StackSpiller.ChildRewriter.AddArguments(IArgumentProvider expressions)
   at System.Linq.Expressions.Compiler.StackSpiller.RewriteMethodCallExpression(Expression expr, Stack stack)
   at System.Linq.Expressions.Compiler.StackSpiller.RewriteExpression(Expression node, Stack stack)
   at System.Linq.Expressions.Compiler.StackSpiller.ChildRewriter.Add(Expression expression)
   at System.Linq.Expressions.Compiler.StackSpiller.ChildRewriter.AddArguments(IArgumentProvider expressions)
   at System.Linq.Expressions.Compiler.StackSpiller.RewriteMethodCallExpression(Expression expr, Stack stack)
   at System.Linq.Expressions.Compiler.StackSpiller.RewriteExpression(Expression node, Stack stack)
   at System.Linq.Expressions.Compiler.StackSpiller.RewriteExpressionFreeTemps(Expression expression, Stack stack)
   at System.Linq.Expressions.Compiler.StackSpiller.Rewrite[T](Expression`1 lambda)
   at System.Linq.Expressions.Expression`1.Accept(StackSpiller spiller)
   at System.Linq.Expressions.Compiler.LambdaCompiler.Compile(LambdaExpression lambda)
   at System.Linq.Expressions.Expression`1.Compile()
   at System.Linq.EnumerableQuery`1.GetEnumerator()
   at System.Linq.EnumerableQuery`1.System.Collections.Generic.IEnumerable<T>.GetEnumerator()
   at System.Linq.Enumerable.SelectEnumerableIterator`2.ToArray()
   at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
   at FlexLabs.EntityFrameworkCore.Upsert.Runners.InMemoryUpsertCommandRunner.FindMatches[TEntity](IEntityType entityType, IEnumerable`1 entities, DbContext dbContext, Expression`1 matchExpression)
   at FlexLabs.EntityFrameworkCore.Upsert.Runners.InMemoryUpsertCommandRunner.RunCore[TEntity](DbContext dbContext, IEntityType entityType, ICollection`1 entities, Expression`1 matchExpression, Expression`1 updateExpression, Expression`1 updateCondition, RunnerQueryOptions queryOptions)
   at FlexLabs.EntityFrameworkCore.Upsert.Runners.InMemoryUpsertCommandRunner.Run[TEntity](DbContext dbContext, IEntityType entityType, ICollection`1 entities, Expression`1 matchExpression, Expression`1 updateExpression, Expression`1 updateCondition, RunnerQueryOptions queryOptions)
   at FlexLabs.EntityFrameworkCore.Upsert.UpsertCommandBuilder`1.Run()

Code that causes this:

dbContext.Set<Entity>().Upsert(entity).On(x => x.Name).Run();

where Entity:


public class Entity
{
	public Guid Id { get; set; }

	public string Name { get; set; } = default!;
}

There’s actually a bit more code there but I am thinking it is not relevant due to this working in .NETCore3.1. However, please do let me know if you require more to help diagnose this issue and I will do my best to see if I can get it to you. 👍

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
artiomchicommented, Feb 27, 2021

Seems like this is a regression in the expression compiler in the InMemory EF Core driver, which is unfortunate.

I was hoping to avoid something like @RomanGolovanov’s solution, as it converts the whole DbSet into an in memory list, but I don’t really see another alternative here.

I’m merging your commit into the project, as it seems to sort out this problem. Thanks for providing it here!

0reactions
tinduongcommented, Feb 27, 2021

@RomanGolovanov Thank you for a quick response. I will try this out…

Read more comments on GitHub >

github_iconTop Results From Across the Web

EF Core in-memory database generate System. ...
I got the following error when I try to test an update operation using Entity Framework core: System.InvalidOperationException : The instance of ...
Read more >
EF Core in-memory database generate System. ...
I got the following error when I try to test an update operation using Entity Framework core: System.InvalidOperationException : The ...
Read more >
Using Entity Framework Core in-memory database for unit ...
I am getting an exception. “Message: System.InvalidOperationException : Unable to resolve service for type 'Microsoft.EntityFrameworkCore.
Read more >
New features for testing your Entity Framework Core 5 and ...
Bad LINQ code: EF Core throws an exception if can't translate my LINQ query into database code. That will make the test fail....
Read more >
Should I check if something exists in the db and fail fast or ...
When assigning ChildId to Parent should I check first if it exists in the DB or wait for the DB to throw an...
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