dotnet / efcore

EF Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations.
https://docs.microsoft.com/ef/
MIT License
13.65k stars 3.15k forks source link

Cosmos: cannot project owned entities in various scenarios #34350

Open maumar opened 1 month ago

maumar commented 1 month ago

query:

public virtual Task Json_projection_second_element_projected_before_owner_nested_as_well_as_root_AsNoTrackingWithIdentityResolution(bool async)
    => AssertQuery(
        async,
        ss => ss.Set<JsonEntityBasic>().Select(
            x => new
            {
                x.Id,
                Duplicate = x.OwnedReferenceRoot.OwnedReferenceBranch.OwnedCollectionLeaf[1],
                Original = x.OwnedReferenceRoot.OwnedReferenceBranch.OwnedCollectionLeaf,
                Parent = x.OwnedReferenceRoot.OwnedReferenceBranch,
                Owner = x
            }).AsNoTrackingWithIdentityResolution(),

exception:

Message: 
System.ArgumentException : Expression of type 'System.Collections.Generic.List`1[Microsoft.EntityFrameworkCore.TestModels.JsonQuery.JsonOwnedLeaf]' cannot be used for parameter of type 'System.Linq.IQueryable`1[Microsoft.EntityFrameworkCore.TestModels.JsonQuery.JsonOwnedLeaf]' of method 'Microsoft.EntityFrameworkCore.TestModels.JsonQuery.JsonOwnedLeaf ElementAt[JsonOwnedLeaf](System.Linq.IQueryable`1[Microsoft.EntityFrameworkCore.TestModels.JsonQuery.JsonOwnedLeaf], Int32)' (Parameter 'arg0')

  Stack Trace: 
ExpressionUtils.ValidateOneArgument(MethodBase method, ExpressionType nodeKind, Expression arguments, ParameterInfo pi, String methodParamName, String argumentParamName, Int32 index)
Expression.Call(Expression instance, MethodInfo method, Expression arg0, Expression arg1)
Expression.Call(Expression instance, MethodInfo method, IEnumerable`1 arguments)
CosmosProjectionBindingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression) line 635
CosmosProjectionBindingExpressionVisitor.Visit(Expression expression) line 141
CosmosProjectionBindingExpressionVisitor.VisitNew(NewExpression newExpression) line 683
CosmosProjectionBindingExpressionVisitor.Visit(Expression expression) line 107
CosmosProjectionBindingExpressionVisitor.Translate(SelectExpression selectExpression, Expression expression) line 76
CosmosQueryableMethodTranslatingExpressionVisitor.TranslateSelect(ShapedQueryExpression source, LambdaExpression selector) line 1027
QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression) line 450
CosmosQueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression) line 247
QueryableMethodTranslatingExpressionVisitor.Translate(Expression expression) line 62
CosmosQueryableMethodTranslatingExpressionVisitor.Translate(Expression expression) line 153
QueryCompilationContext.CreateQueryExecutorExpression[TResult](Expression query) line 231
QueryCompilationContext.CreateQueryExecutor[TResult](Expression query) line 199
Database.CompileQuery[TResult](Expression query, Boolean async) line 68
QueryCompiler.CompileQueryCore[TResult](IDatabase database, Expression query, IModel model, Boolean async) line 127
<>c__DisplayClass11_0`1.<ExecuteCore>b__0() line 83
CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func`1 compiler) line 66
QueryCompiler.ExecuteCore[TResult](Expression query, Boolean async, CancellationToken cancellationToken) line 79
QueryCompiler.Execute[TResult](Expression query) line 60
EntityQueryProvider.Execute[TResult](Expression expression) line 64
EntityQueryable`1.GetEnumerator() line 78
List`1.ctor(IEnumerable`1 collection)
maumar commented 1 month ago

simpler query:

public virtual Task Json_collection_index_outside_bounds2(bool async)
    => AssertQuery(
        async,
        ss => ss.Set<JsonEntityBasic>().Select(x => x.OwnedReferenceRoot.OwnedReferenceBranch.OwnedCollectionLeaf[25]).AsNoTracking(),
        ss => ss.Set<JsonEntityBasic>().Select(x => (JsonOwnedLeaf)null));
roji commented 5 days ago

Note: some partial work was done on this in #34662 - leaf entity types can now be projected without errors. However, non-leaf entity types - which have Includes on them - still have the issue. I'm expecting this issue to go away on its own with the switch to complex type modeling.