Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

invalidcastexception in unit tests using in memory on count, firstordefault, of many to many related entities #26893

Closed
dalebeutel opened this issue Dec 3, 2021 · 5 comments

Comments

@dalebeutel
Copy link

dalebeutel commented Dec 3, 2021

EF Core version: 5.0.11
Database provider: Microsoft.EntityFrameworkCore.InMemory 5.0.9
Target framework: (e.g. dotnet core 3.1)
Operating system: windows 7
IDE: (e.g. Visual Studio 2019 16.3)

I am seeing the following exception on All my many to many entity relationships.
Our Schema is large and was revved up from EF6 to EFCore in late 2020. the many to many relationships work correctly when accessing a SQL database in MSSQL 2019

This is caused but this call

dbContext.DbSet<Parent>().Include(x=>x.Children).Select(x => new { x.Id, x.Children, cnt = x.Children.Count() }).ToList();

where Parent and Children have a Many to Many relationship.

when run as

dbContext.DbSet<Parent>().Include(x=>x.Children).Select(x => new { x.Id, x.Children}).ToList(); 

the exception does not occur and x.Children is not null and has the correct entries.

This is only effecting our unit test development.

Exception:
System.InvalidCastException
HResult=0x80004002
Message=Unable to cast object of type 'System.Linq.Expressions.NewExpression' to type 'System.Linq.Expressions.MethodCallExpression'.
Source=Microsoft.EntityFrameworkCore.InMemory
StackTrace:
at Microsoft.EntityFrameworkCore.InMemory.Query.Internal.InMemoryQueryExpression.AddSubqueryProjection(ShapedQueryExpression shapedQueryExpression, Expression& innerShaper)
at Microsoft.EntityFrameworkCore.InMemory.Query.Internal.InMemoryProjectionBindingExpressionVisitor.Visit(Expression expression)
at Microsoft.EntityFrameworkCore.InMemory.Query.Internal.InMemoryProjectionBindingExpressionVisitor.VisitNew(NewExpression newExpression)
at System.Linq.Expressions.NewExpression.Accept(ExpressionVisitor visitor)
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
at Microsoft.EntityFrameworkCore.InMemory.Query.Internal.InMemoryProjectionBindingExpressionVisitor.Visit(Expression expression)
at Microsoft.EntityFrameworkCore.InMemory.Query.Internal.InMemoryProjectionBindingExpressionVisitor.Translate(InMemoryQueryExpression queryExpression, Expression expression)
at Microsoft.EntityFrameworkCore.InMemory.Query.Internal.InMemoryQueryableMethodTranslatingExpressionVisitor.TranslateSelect(ShapedQueryExpression source, LambdaExpression selector)
at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
at Microsoft.EntityFrameworkCore.InMemory.Query.Internal.InMemoryQueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
at Microsoft.EntityFrameworkCore.Query.QueryCompilationContext.CreateQueryExecutor[TResult](Expression query)
at Microsoft.EntityFrameworkCore.Storage.Database.CompileQuery[TResult](Expression query, Boolean async)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[TResult](IDatabase database, Expression query, IModel model, Boolean async)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass9_01.<Execute>b__0() at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func1 compiler)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query)
at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression)
at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable1.GetEnumerator() at System.Collections.Generic.List1..ctor(IEnumerable1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable1 source)
at ABSolute.Core.Module.DealManagement.DataServices.Test.FundingCommitmentBrowser.FundingCommitmentBrowserDataServiceTest.FundingCommitmentBrowserDataService_LoadConduits() in I:\Development\DEv3\CoreServer\Modules\Tests\ABSolute.Core.Module.DealManagement.DataServices.Test\FundingCommitmentBrowser\FundingCommitmentBrowserDataServiceTest.cs:line 33

Your Assistance is Appreciated.

@ajcvickers
Copy link
Contributor

Possible duplicate of #17620.

@ajcvickers
Copy link
Contributor

@dalebeutel We believe this is fixed in EF Core 6.0. Please report back if this is still not working for you in 6.0.

@dalebeutel
Copy link
Author

@ajcvickers I will try to rev forward to EF Core 6, I must first rev my entire project to net 5.0, we are currently on Core 3.1.
Is EF Core 5.x closed or might the in memory provider fix percolate down?

@ajcvickers
Copy link
Contributor

@dalebeutel It's very unlikely that this will be backported to 5.0, since this issue generally doesn't meet the criteria for a patch, and also 5.0 is a non-LTS release which goes out-of-support in May.

@dalebeutel
Copy link
Author

@ajcvickers , understood, thank you

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants