Repository navigation
Exception when using FromSql on entity with ComplexProperty #32699
Copy link
Copy link
Milestone
Description
Reproduction: EfComplexPropertyFromSqlTests.zip
Given an entity like the following:
public class Entity
{
public required ComplexPropertyType ComplexProperty { get; set; }
public Guid Id { get; set; } = Guid.NewGuid();
}
[ComplexType]
public record ComplexPropertyType(
string Property1,
string Property2);
Which works fine when inserting and querying normally.
The problem occurs when trying to execute a FromSql query like so:
db.Entities.FromSql($"""SELECT * FROM Entities""").ToList();
Or similar (see the DoThis service in the attached code).
The following exception is thrown when running the attached code:
System.Collections.Generic.KeyNotFoundException: The given key 'Table: Entities Key: PK_Entities {'Id'} PrimaryKey' was not present in the dictionary.
at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
at Microsoft.EntityFrameworkCore.Query.SqlExpressions.SelectExpression.GenerateComplexPropertyShaperExpression(StructuralTypeProjectionExpression containerProjection, IComplexProperty complexProperty)
at Microsoft.EntityFrameworkCore.Query.StructuralTypeProjectionExpression.BindComplexProperty(IComplexProperty complexProperty)
at Microsoft.EntityFrameworkCore.Query.RelationalSqlTranslatingExpressionVisitor.BindComplexProperty(StructuralTypeReferenceExpression typeReference, IComplexProperty complexProperty)
at Microsoft.EntityFrameworkCore.Query.RelationalSqlTranslatingExpressionVisitor.TryBindMember(Expression source, MemberIdentity member, Expression& expression, IPropertyBase& property)
at Microsoft.EntityFrameworkCore.Query.RelationalSqlTranslatingExpressionVisitor.TryBindMember(Expression source, MemberIdentity member, Expression& expression)
at Microsoft.EntityFrameworkCore.Query.RelationalSqlTranslatingExpressionVisitor.VisitMember(MemberExpression memberExpression)
at Microsoft.EntityFrameworkCore.Query.RelationalSqlTranslatingExpressionVisitor.TranslateInternal(Expression expression, Boolean applyDefaultTypeMapping)
at Microsoft.EntityFrameworkCore.Query.RelationalSqlTranslatingExpressionVisitor.TranslateProjection(Expression expression, Boolean applyDefaultTypeMapping)
at Microsoft.EntityFrameworkCore.Query.Internal.RelationalProjectionBindingExpressionVisitor.Visit(Expression expression)
at Microsoft.EntityFrameworkCore.Query.Internal.RelationalProjectionBindingExpressionVisitor.VisitNew(NewExpression newExpression)
at Microsoft.EntityFrameworkCore.Query.Internal.RelationalProjectionBindingExpressionVisitor.Visit(Expression expression)
at Microsoft.EntityFrameworkCore.Query.Internal.RelationalProjectionBindingExpressionVisitor.Translate(SelectExpression selectExpression, Expression expression)
at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.TranslateSelect(ShapedQueryExpression source, LambdaExpression selector)
at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.Translate(Expression expression)
at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.Translate(Expression expression)
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.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)
at EfComplexPropertyFromSqlTests.DoThis.Log[T](String name, IQueryable`1 queryable) in C:\Users\kim\source\tests\EfComplexPropertyFromSqlTests\EfComplexPropertyFromSqlTests\DoThis.cs:line 49
at EfComplexPropertyFromSqlTests.DoThis.ExecuteAsync(CancellationToken stoppingToken) in C:\Users\kim\source\tests\EfComplexPropertyFromSqlTests\EfComplexPropertyFromSqlTests\DoThis.cs:line 40
at Microsoft.Extensions.Hosting.Internal.Host.TryExecuteBackgroundServiceAsync(BackgroundService backgroundService)
Provider and version information
EF Core version: 8
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 8.0.100
Operating system: Windows 11
Reacted by Ramhari Gyawali, AlexTankSWO and Alex
Activity
Use table mappings for FromSql
Add complex type mappings to the default relational mappings
Add complex type mappings to the default relational mappings
Add complex type mappings to the default relational mappings (#32867)
Add complex type mappings to the default relational mappings (#32867)
Add complex type mappings to the default relational mappings (#32867)
[release/8.0] Add complex type mappings to the default relational map…
samisq commentedon Feb 20, 2024
I encountered the exact same exception when using
FromExpressionthat maps to a table-Valued function, instead ofFromSql, like:Will this fix address it too?
AlexTankSWO commentedon Sep 11, 2024
I am facing the same issue. As soon as I remove my complex type property from my entity,
FromSqlworks as expected.We are using EF Core 8.0.1
But I could see that in 8.0.8 this issue does not occur anymore.
AndriySvyryd commentedon Sep 13, 2024
@samisq No, that's tracked in #34627
Bump Microsoft.EntityFrameworkCore.Design and System.Text.Json (#60)