Projection fails with `The given key 'BId' was not present in the dictionary` if `AOnlyProperty` is present in the destination `ADto`
Originally filed with AutoMapper: https://github.com/AutoMapper/AutoMapper/issues/4389
Description
Using the setup described in the attached gist, the query (see gist) fails with the attached stack trace.
However, removing the ADto.AOnlyProperty property causes the query to succeed.
Code
https://gist.github.com/FelixZY/a98b1531c8eabd96d30dd4729be44cdd
Stack traces
Error Message:
System.Collections.Generic.KeyNotFoundException : The given key 'BId' was not present in the dictionary.
Stack Trace:
at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
at Microsoft.EntityFrameworkCore.Query.SqlExpressions.SelectExpression.ProjectionMemberToIndexConvertingExpressionVisitor.Visit(Expression expression)
at System.Linq.Expressions.ExpressionVisitor.VisitUnary(UnaryExpression node)
at Microsoft.EntityFrameworkCore.Query.SqlExpressions.SelectExpression.ProjectionMemberToIndexConvertingExpressionVisitor.Visit(Expression expression)
at System.Linq.Expressions.ExpressionVisitor.VisitMemberAssignment(MemberAssignment node)
at System.Linq.Expressions.ExpressionVisitor.VisitMemberBinding(MemberBinding node)
at System.Linq.Expressions.ExpressionVisitor.Visit[T](ReadOnlyCollection`1 nodes, Func`2 elementVisitor)
at System.Linq.Expressions.ExpressionVisitor.VisitMemberInit(MemberInitExpression node)
at Microsoft.EntityFrameworkCore.Query.SqlExpressions.SelectExpression.ProjectionMemberToIndexConvertingExpressionVisitor.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 Pomelo.EntityFrameworkCore.MySql.Query.ExpressionVisitors.Internal.MySqlQueryTranslationPostprocessor.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__DisplayClass12_0`1.<ExecuteAsync>b__0()
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.EntityFrameworkQueryableExtensions.ExecuteAsync[TSource,TResult](MethodInfo operatorMethodInfo, IQueryable`1 source, Expression expression, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ExecuteAsync[TSource,TResult](MethodInfo operatorMethodInfo, IQueryable`1 source, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.SingleAsync[TSource](IQueryable`1 source, CancellationToken cancellationToken)
at AutoMapperExample.Example() in [REDACTED]
at System.Threading.Tasks.Task.<>c.<ThrowAsync>b__128_0(Object state)
Include provider and version information
EF Core version: 8.0.1
Database provider: Pomelo.EntityFrameworkCore.MySql 8.0.0-beta.2
Target framework: net8.0
Operating system: mcr.microsoft.com/devcontainers/dotnet:8.0 running on Ubuntu 22.04.3 LTS (jammy) (64-bit)
IDE: VSCode/dotnet cli
@FelixZY The stack trace above is from EF Core, but the repro code uses EF6. It's not clear to me how it's possible to get that stack trace from the repro code you posted.
@ajcvickers I originally used the template provided by AutoMapper but have now updated the gist to be a completely self-contained EF Core project. The database backend has also been updated from MySql to SqlServer as I had some issues getting MySql to connect. The issue is still reproducible on SqlServer though.
Note for team: still fails on latest daily build; not a regression.