Skip to content

Duplicate table alias in generated select query (An item with the same key has already been added) #30358

@OldrichDlouhy

Description

@OldrichDlouhy

EF Core Query results in argument exception An item with the same key has already been added. Key: c0

caused by generated duplicate table alias

Code

Query causing the problem:

        var query = dbContext.ContainerConfigurations
            .AsSplitQuery()
            .Include(e => e.Container)
                .ThenInclude(e => e.HardwareUnit)
                    .ThenInclude(e => e.CurrentConfiguration)
            .Where(e => e.Fraction == entity);

        query.ToQueryString();

Solution with test failing with duplicate table alias:

EFCoreDuplicateTableAlias.zip

The issue manifested after upgrading from EF Core 6 to EF Core 7.
With MySQL Pomelo provider, the issue manifests both for split and non-split query.
For SQLite provider (unit test) it fails with split query.

It may be related to global query filters as removing the filters makes the issue go away.

Stack trace

System.ArgumentException
  HResult=0x80070057
  Message=An item with the same key has already been added. Key: c0
  Source=System.Private.CoreLib
  StackTrace:
   at System.ThrowHelper.ThrowAddingDuplicateWithKeyArgumentException[T](T key)
   at System.Collections.Generic.Dictionary`2.TryInsert(TKey key, TValue value, InsertionBehavior behavior)
   at System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value)
   at System.Linq.Enumerable.ToDictionary[TSource,TKey](List`1 source, Func`2 keySelector, IEqualityComparer`1 comparer)
   at Microsoft.EntityFrameworkCore.Query.SqlExpressions.SelectExpression.ColumnExpressionReplacingExpressionVisitor..ctor(SelectExpression oldSelectExpression, IEnumerable`1 newTableReferences)
   at Microsoft.EntityFrameworkCore.Query.SqlExpressions.SelectExpression.CloningExpressionVisitor.Visit(Expression expression)
   at Microsoft.EntityFrameworkCore.Query.SqlExpressions.InnerJoinExpression.VisitChildren(ExpressionVisitor visitor)
   at Microsoft.EntityFrameworkCore.Query.SqlExpressions.SelectExpression.CloningExpressionVisitor.Visit(Expression expression)
   at System.Linq.Enumerable.SelectListIterator`2.MoveNext()
   at System.Linq.Enumerable.<OfTypeIterator>d__65`1.MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at Microsoft.EntityFrameworkCore.Utilities.EnumerableExtensions.ToList[TSource](IEnumerable source)
   at Microsoft.EntityFrameworkCore.Query.SqlExpressions.SelectExpression.CloningExpressionVisitor.Visit(Expression expression)
   at Microsoft.EntityFrameworkCore.Query.SqlExpressions.SelectExpression.ApplyProjection(Expression shaperExpression, ResultCardinality resultCardinality, QuerySplittingBehavior querySplittingBehavior)
   at Microsoft.EntityFrameworkCore.Query.Internal.SelectExpressionProjectionApplyingExpressionVisitor.VisitExtension(Expression extensionExpression)
   at Microsoft.EntityFrameworkCore.Query.RelationalQueryTranslationPostprocessor.Process(Expression query)
   at Microsoft.EntityFrameworkCore.Sqlite.Query.Internal.SqliteQueryTranslationPostprocessor.Process(Expression query)
   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_0`1.<Execute>b__0()
   at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func`1 compiler)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query)
   at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression)
   at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ToQueryString(IQueryable source)
   at eGateDigi.Web.Services.Containers.Configuration.UpdateFractionHandler.<Handle>d__3.MoveNext() in C:\Code\eGateDigi\eGateDigi.Web\UpdateFraction.cs:line 89

Provider and version information

EF Core version: 7.0.3
Database provider: Microsoft.EntityFrameworkCore.SqLite 7.0.3
Target framework: .NET 7.0
Operating system: Windows 10 19044.2604
IDE: Visual Studio 2022 17.4

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions