Efcore: System.ArgumentOutOfRangeException: Index was out of range on AsSplitQuery when using Union

Created on 13 Nov 2020  路  7Comments  路  Source: dotnet/efcore

Hello Team, I'm having this exception:

Exception: System.ArgumentOutOfRangeException: Index was out of range.

Triying to use AsSplitQuery() on a query like this:

I have a property on the context class called MyStuff, if I have a specific permission I will get all the entities, if not only the ones that belong to me and the ones assigned to me (resolved with an Union)

public IQueryable<Stuff> MyStuff
        {
            get
            {
                if (this.usersService.UserHasPermission("all:stuff"))
                {
                    return this.Stuff;
                }

                var userId = this.usersService.GetUserId();

                var stuff1= this.Stuff
                    .Where(x =>
                        x.CreatedByUserId == userId);

                var assignedStuff=
                    from cs in this.Stuff
                    from an in cs.Assignations
                    where an.AssignedUserId == userId
                    select cs;

                return assignedStuff
                    .Union(stuff1);
            }
        }
var query = dbContext.MyStuff
                .AsNoTracking()
                .AsSplitQuery()
                .Include(x => x.Ref1)
                .Include(x => x.Ref2)
                .Include(x => x.Ref3)
                .Include(x => x.Ref4)
                .ThenInclude(x => x.SubRef41)
                .Include(x => x.Ref5)
                .ThenInclude(x => x.SubRef51)
                .Include(x => x.Ref6)
                .ThenInclude(x => x.SubRef61).ToList()

When running with the permission, this works fine, but when I got the main query with an union, I got this exception:

Exception: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index') at Microsoft.EntityFrameworkCore.Query.SqlExpressions.SelectExpression.ApplyCollectionJoin(Int32 collectionIndex, Int32 collectionId, Expression innerShaper, INavigationBase navigation, Type elementType, Boolean splitQuery) at Microsoft.EntityFrameworkCore.Query.Internal.CollectionJoinApplyingExpressionVisitor.VisitExtension(Expression extensionExpression) at Microsoft.EntityFrameworkCore.Query.IncludeExpression.VisitChildren(ExpressionVisitor visitor) at Microsoft.EntityFrameworkCore.Query.Internal.CollectionJoinApplyingExpressionVisitor.VisitExtension(Expression extensionExpression) at Microsoft.EntityFrameworkCore.Query.IncludeExpression.VisitChildren(ExpressionVisitor visitor) at Microsoft.EntityFrameworkCore.Query.Internal.CollectionJoinApplyingExpressionVisitor.VisitExtension(Expression extensionExpression) at Microsoft.EntityFrameworkCore.Query.IncludeExpression.VisitChildren(ExpressionVisitor visitor) at Microsoft.EntityFrameworkCore.Query.Internal.CollectionJoinApplyingExpressionVisitor.VisitExtension(Expression extensionExpression) at Microsoft.EntityFrameworkCore.Query.IncludeExpression.VisitChildren(ExpressionVisitor visitor) at Microsoft.EntityFrameworkCore.Query.Internal.CollectionJoinApplyingExpressionVisitor.VisitExtension(Expression extensionExpression) at Microsoft.EntityFrameworkCore.Query.Internal.CollectionJoinApplyingExpressionVisitor.VisitExtension(Expression extensionExpression) at Microsoft.EntityFrameworkCore.Query.RelationalQueryTranslationPostprocessor.Process(Expression query) at Microsoft.EntityFrameworkCore.Query.QueryCompilationContext.CreateQueryExecutor[TResult](Expression query) at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func`1 compiler) at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.ExecuteAsync[TResult](Expression query, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.ExecuteAsync[TResult](Expression expression, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1.GetAsyncEnumerator(CancellationToken cancellationToken) at System.Runtime.CompilerServices.ConfiguredCancelableAsyncEnumerable`1.GetAsyncEnumerator() at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ToListAsync[TSource](IQueryable`1 source, CancellationToken cancellationToken)

EF Core version: 5
Database provider: Microsoft.EntityFrameworkCore.SqlServe)
Target framework: .NET 5.0)
Operating system: Windows

Thanks

closed-duplicate customer-reported

Most helpful comment

I cant share what I'm working right now, but I can try to create a new small project with the same idea.. Will post here back when is ready.

All 7 comments

Can you share a runnable repro code?

I cant share what I'm working right now, but I can try to create a new small project with the same idea.. Will post here back when is ready.

I think is related to this.
https://github.com/dotnet/efcore/pull/23273
I tried to reproduce on a new environment with no success, but what I can tell you that is happening when I try to Include on the query the reference to a Collection that has a composed key.

Like this:

            modelBuilder.Entity<Ref1>()
                .HasKey(cf => new { cf.StuffId, cf.UserId });

Without the include to Ref1, works fine for now... Will keep taking a look

Thanks!

Update!
I replaced the composed key with a Unique Id PK, and now the query with AsSplitQuery is working fine...

Duplicate of #23211

cc: @ajcvickers - Customer report

Now is happening with another different reference that is not a composite unique id..., I will try to create something to reproduce it. Thanks

EF Team Triage: Closing this issue as the requested additional details have not been provided and we have been unable to reproduce it.

BTW this is a canned response and may have info or details that do not directly apply to this particular issue. While we'd like to spend the time to uniquely address every incoming issue, we get a lot traffic on the EF projects and that is not practical. To ensure we maximize the time we have to work on fixing bugs, implementing new features, etc. we use canned responses for common triage decisions.

Was this page helpful?
0 / 5 - 0 ratings