{"record":{"id":"62d6fd63a5b0c988","repo":"dotnet/efcore","slug":"the-element-type-elementtype-used-in-sqlquery","errorCode":null,"errorMessage":"The element type '{elementType}' used in 'SqlQuery' method is not natively supported by your database provider. Either use a supported element type, or use ModelConfigurationBuilder.DefaultTypeMapping to define a mapping for your type.","messagePattern":"The element type '(.+?)' used in 'SqlQuery' method is not natively supported by your database provider\\. Either use a supported element type, or use ModelConfigurationBuilder\\.DefaultTypeMapping to define a mapping for your type\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Relational/Query/RelationalQueryableMethodTranslatingExpressionVisitor.cs","lineNumber":222,"sourceCode":"                Check.DebugAssert(\n                    !subquerySourceSelectExpression.HasNonEntityNullabilityMarkers,\n                    \"Non-grouping subquery clone carries a live non-entity nullability marker; it would be stranded by Clone(). \"\n                    + \"Route it through a marker-aware remap as the GroupByShaperExpression case does.\");\n\n                var clonedSelectExpression = subquerySourceSelectExpression.Clone();\n                return new ShapedQueryExpression(\n                    clonedSelectExpression,\n                    new QueryExpressionReplacingExpressionVisitor(shapedQueryExpression.QueryExpression, clonedSelectExpression)\n                        .Visit(shapedQueryExpression.ShaperExpression));\n\n            case SqlQueryRootExpression sqlQueryRootExpression:\n            {\n                var typeMapping = RelationalDependencies.TypeMappingSource.FindMapping(\n                    sqlQueryRootExpression.ElementType, RelationalDependencies.Model);\n\n                if (typeMapping == null)\n                {\n                    throw new InvalidOperationException(\n                        RelationalStrings.SqlQueryUnmappedType(sqlQueryRootExpression.ElementType.DisplayName()));\n                }\n\n                var alias = _sqlAliasManager.GenerateTableAlias(\"sql\");\n                var selectExpression = new SelectExpression(\n                    [new FromSqlExpression(alias, sqlQueryRootExpression.Sql, sqlQueryRootExpression.Argument)],\n                    new ColumnExpression(\n                        SqlQuerySingleColumnAlias,\n                        alias,\n                        sqlQueryRootExpression.Type.UnwrapNullableType(),\n                        typeMapping,\n                        sqlQueryRootExpression.Type.IsNullableType()),\n                    identifier: [],\n                    _sqlAliasManager);\n\n                Expression shaperExpression = new ProjectionBindingExpression(\n                    selectExpression, new ProjectionMember(), sqlQueryRootExpression.ElementType.MakeNullable());\n","sourceCodeStart":204,"sourceCodeEnd":240,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Relational/Query/RelationalQueryableMethodTranslatingExpressionVisitor.cs#L204-L240","documentation":"Thrown when SqlQuery<T> (FromSqlRaw / SqlQueryRaw-style raw SQL returning scalars) is used with an element type for which the relational type mapping source has no mapping. EF Core needs a type mapping to read the single column the raw SQL returns.","triggerScenarios":"Calling context.Database.SqlQueryRaw<MyCustomType>(...) or SqlQuery<MyType>(...) where MyType is not a primitive or otherwise unmapped type. The TypeMappingSource.FindMapping call returns null and the error fires.","commonSituations":"Using a custom struct/class, an enum without mapping, a nullable value type not configured, or a type that requires a value converter but none is registered via DefaultTypeMapping.","solutions":["Use a supported primitive element type (int, string, Guid, DateTime, etc.).","Register a default type mapping for the custom type: modelConfigurationBuilder.DefaultTypeMapping<MyCustomType>().","If the type is an enum, configure its conversion/mapping explicitly.","Map the result to an entity type via FromSqlRaw instead of SqlQuery<T> if the row is multi-column or complex."],"exampleFix":"// before\nvar ids = ctx.Database.SqlQueryRaw<MyCustomStruct>(\"SELECT x FROM t\");\n// after\nprotected override void ConfigureConventions(ModelConfigurationBuilder b)\n    => b.DefaultTypeMapping<MyCustomStruct>().HasConversion<MyCustomStructConverter>();\n// or use a primitive\nvar ids = ctx.Database.SqlQueryRaw<int>(\"SELECT x FROM t\");","handlingStrategy":"validation","validationCode":"// Verify a type mapping exists for the SqlQuery element type before use.\nvar mapping = ctx.GetService<IRelationalTypeMappingSource>()\n    .FindMapping(typeof(MyCustomStruct), ctx.Model);\nif (mapping is null)\n    throw new InvalidOperationException($\"No type mapping for {typeof(MyCustomStruct).Name}; register via DefaultTypeMapping.\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use primitive element types with SqlQuery<T>.","Register DefaultTypeMapping<T> for custom or enum element types.","Prefer FromSqlRaw mapping to an entity type for multi-column results."],"tags":["efcore","sqlquery","type-mapping","raw-sql"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}