Repository navigation
Closed
Description
query:
from l1 in ss.Set<Level1>()
orderby l1.Id
let inner = (from l2 in l1.OneToMany_Optional1
where l2.Name != "Foo"
let innerL1s = from innerL1 in ss.Set<Level1>()
where innerL1.OneToMany_Optional1.Any(innerL2 => innerL2.Id == l2.Id)
select innerL1.Name
select innerL1s.ToList()).FirstOrDefault()
select inner.ToList()exception:
System.ArgumentException : GenericArguments[2], 'System.Collections.Generic.List`1[System.String]', on 'Void PopulateCollection[TCollection,TElement,TRelatedEntity](Int32, Microsoft.EntityFrameworkCore.Query.QueryContext, System.Data.Common.DbDataReader, Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryResultCoordinator, System.Func`3[Microsoft.EntityFrameworkCore.Query.QueryContext,System.Data.Common.DbDataReader,System.Object[]], System.Func`3[Microsoft.EntityFrameworkCore.Query.QueryContext,System.Data.Common.DbDataReader,System.Object[]], System.Func`3[Microsoft.EntityFrameworkCore.Query.QueryContext,System.Data.Common.DbDataReader,System.Object[]], System.Collections.Generic.IReadOnlyList`1[Microsoft.EntityFrameworkCore.ChangeTracking.ValueComparer], System.Collections.Generic.IReadOnlyList`1[Microsoft.EntityFrameworkCore.ChangeTracking.ValueComparer], System.Collections.Generic.IReadOnlyList`1[Microsoft.EntityFrameworkCore.ChangeTracking.ValueComparer], System.Func`5[Microsoft.EntityFrameworkCore.Query.QueryContext,System.Data.Common.DbDataReader,Microsoft.EntityFrameworkCore.Query.Internal.ResultContext,Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryResultCoordinator,TRelatedEntity])' violates the constraint of type 'TRelatedEntity'.
---- System.Security.VerificationException : Method Microsoft.EntityFrameworkCore.Query.RelationalShapedQueryCompilingExpressionVisitor+ShaperProcessingExpressionVisitor.PopulateCollection: type argument 'System.Collections.Generic.List`1[System.String]' violates the constraint of type parameter 'TRelatedEntity'.
Stack Trace:
RuntimeType.ValidateGenericArguments(MemberInfo definition, RuntimeType[] genericArguments, Exception e)
RuntimeMethodInfo.MakeGenericMethod(Type[] methodInstantiation)
ShaperProcessingExpressionVisitor.VisitExtension(Expression extensionExpression) line 757
Expression.Accept(ExpressionVisitor visitor)
ExpressionVisitor.Visit(Expression node)
ShaperProcessingExpressionVisitor.ProcessShaper(Expression shaperExpression, RelationalCommandCache& relationalCommandCache, LambdaExpression& relatedDataLoaders) line 279
RelationalShapedQueryCompilingExpressionVisitor.VisitShapedQuery(ShapedQueryExpression shapedQueryExpression) line 66
ShapedQueryCompilingExpressionVisitor.VisitExtension(Expression extensionExpression) line 97
Expression.Accept(ExpressionVisitor visitor)
ExpressionVisitor.Visit(Expression node)
QueryCompilationContext.CreateQueryExecutor[TResult](Expression query) line 181
Database.CompileQuery[TResult](Expression query, Boolean async) line 72
QueryCompiler.CompileQueryCore[TResult](IDatabase database, Expression query, IModel model, Boolean async) line 114
<>c__DisplayClass9_0`1.<Execute>b__0() line 98
CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func`1 compiler) line 78
QueryCompiler.Execute[TResult](Expression query) line 94
EntityQueryProvider.Execute[TResult](Expression expression) line 81
EntityQueryable`1.GetEnumerator() line 93
Activity
maumar commentedon Nov 13, 2020
related to #23205
maumar commentedon Nov 13, 2020
Note: for weak entities model, this query throws different exception:
smitpatel commentedon Nov 25, 2020
@maumar
Is in our current tests and works for all providers (Sqlite throws Apply not supported exception). The test looks awfully similar
maumar commentedon Mar 23, 2021
@smitpatel the only difference is top level ToList (on inner which is already converted to ToList in the let). Issue happens when processing RelationalCollectionShaper that already has a nested RelationalCollectionShaper. The query that works only has one of these so we don't hit the issue.
Shaper for working query:
shaper for failing query:
smitpatel commentedon Aug 30, 2021
Removing from triage to decide what do we want to do for 6.0 here
Notes from investigation
In current latest code, we throw exception that parameter source is null. The root cause is the
ToListcall on the list object. While debugging I verified that we do have empty list object, I am not able to figure out why calling ToList over that is throwing parameter null exception.Shaper
It is the ToList call in very last line. resultContext.Values[1] seems to have empty list while evaluating PopulateCollection function, not sure what goes wrong afterwards. It shouldn't get contaminated else we would see a lot more errors in the query.
Cost of figuring out what is wrong may not be worth iff we remove the redundant ToList. We already have test which works when user specify only 1
ToList(either inner or outer doesn't matter).So decision to make
ajcvickers commentedon Aug 31, 2021
Note from triage: backlogging to remove the redundant ToList, but as a low-priority fix.
smitpatel commentedon May 19, 2022
Revisiting this -> Actually above output was bug in expression printer (already notified authorities).
The result of FirstOrDefault in the query is null since the enumerable is empty. And we call ToList on this result so ArgumentNullException is by design here.
Update test for #23303
Update test for #23303
Update test for #23303 (#28059)