dotnet/efcore · error · InvalidOperationException
The element type '{elementType}' used in 'SqlQuery' method i
Error message
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. What it means
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.
Source
Thrown at src/EFCore.Relational/Query/RelationalQueryableMethodTranslatingExpressionVisitor.cs:222
Check.DebugAssert(
!subquerySourceSelectExpression.HasNonEntityNullabilityMarkers,
"Non-grouping subquery clone carries a live non-entity nullability marker; it would be stranded by Clone(). "
+ "Route it through a marker-aware remap as the GroupByShaperExpression case does.");
var clonedSelectExpression = subquerySourceSelectExpression.Clone();
return new ShapedQueryExpression(
clonedSelectExpression,
new QueryExpressionReplacingExpressionVisitor(shapedQueryExpression.QueryExpression, clonedSelectExpression)
.Visit(shapedQueryExpression.ShaperExpression));
case SqlQueryRootExpression sqlQueryRootExpression:
{
var typeMapping = RelationalDependencies.TypeMappingSource.FindMapping(
sqlQueryRootExpression.ElementType, RelationalDependencies.Model);
if (typeMapping == null)
{
throw new InvalidOperationException(
RelationalStrings.SqlQueryUnmappedType(sqlQueryRootExpression.ElementType.DisplayName()));
}
var alias = _sqlAliasManager.GenerateTableAlias("sql");
var selectExpression = new SelectExpression(
[new FromSqlExpression(alias, sqlQueryRootExpression.Sql, sqlQueryRootExpression.Argument)],
new ColumnExpression(
SqlQuerySingleColumnAlias,
alias,
sqlQueryRootExpression.Type.UnwrapNullableType(),
typeMapping,
sqlQueryRootExpression.Type.IsNullableType()),
identifier: [],
_sqlAliasManager);
Expression shaperExpression = new ProjectionBindingExpression(
selectExpression, new ProjectionMember(), sqlQueryRootExpression.ElementType.MakeNullable());
View on GitHub (pinned to 3a2006ef56)
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.
Example fix
// before
var ids = ctx.Database.SqlQueryRaw<MyCustomStruct>("SELECT x FROM t");
// after
protected override void ConfigureConventions(ModelConfigurationBuilder b)
=> b.DefaultTypeMapping<MyCustomStruct>().HasConversion<MyCustomStructConverter>();
// or use a primitive
var ids = ctx.Database.SqlQueryRaw<int>("SELECT x FROM t"); Defensive patterns
Strategy: validation
Validate before calling
// Verify a type mapping exists for the SqlQuery element type before use.
var mapping = ctx.GetService<IRelationalTypeMappingSource>()
.FindMapping(typeof(MyCustomStruct), ctx.Model);
if (mapping is null)
throw new InvalidOperationException($"No type mapping for {typeof(MyCustomStruct).Name}; register via DefaultTypeMapping."); Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- The parameter '{parameter}' for the DbFunction '{function}'
- The type of the etag property '{property}' on '{entityType}'
- The type '{clrType}' is being used as a vector, but the vect
- The 'VectorDistance' function can only be used with a proper
- Property '{entityType}.{property}' was configured for full-t
AI-assisted analysis of dotnet/efcore@3a2006ef56 (2026-08-11).
Data as JSON: /api/errors/62d6fd63a5b0c988.
Report an issue: GitHub.