dotnet/efcore · error · NotSupportedException
Encountered a constant of unsupported type '{value.GetType()
Error message
Encountered a constant of unsupported type '{value.GetType().Name}'. Only primitive constant nodes are supported.
{value} What it means
Thrown by GenerateUnknownValue when a constant value is not a default value of its type, not an IRelationalQuotableExpression, and not otherwise renderable. The translator can only emit constants it knows how to render as literals (primitives, enums, tuples, null/default). NotSupportedException. Part of EF Core's LINQ-to-C# syntax translation.
Source
Thrown at src/EFCore.Design/Query/Internal/LinqToCSharpSyntaxTranslator.cs:1188
}
}
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
protected virtual ExpressionSyntax GenerateUnknownValue(object value)
{
var type = value.GetType();
return type.IsValueType
&& value.Equals(type.GetDefaultValue())
? DefaultExpression(Generate(type))
: value is IRelationalQuotableExpression relationalQuotableExpression
&& Translate(relationalQuotableExpression.Quote()) is ExpressionSyntax expressionSyntax
? expressionSyntax
: throw new NotSupportedException(
$"Encountered a constant of unsupported type '{value.GetType().Name}'. Only primitive constant nodes are supported."
+ Environment.NewLine
+ value);
}
/// <inheritdoc />
protected override Expression VisitDebugInfo(DebugInfoExpression node)
=> throw new NotSupportedException("DebugInfo nodes are not supporting when translating expression trees to C#");
/// <inheritdoc />
protected override Expression VisitDefault(DefaultExpression node)
{
Result = DefaultExpression(Generate(node.Type));
return node;
}
/// <inheritdoc />View on GitHub (pinned to dbf9771522)
Solutions
- Provide a constantReplacements map that substitutes the object for a variable name declared in the surrounding generated method.
- Implement IRelationalQuotableExpression on the type so it can be quoted into an expression.
- Avoid capturing non-primitive constants; pass them as parameters instead.
Example fix
// before Expression.Constant(myComplexObject) // after // register a constant replacement so the translator emits the variable name: // constantReplacements[myComplexObject] = "myComplexObject"
Defensive patterns
Strategy: validation
Validate before calling
// Pre-scan for non-primitive constants and register replacements
var replacements = new Dictionary<object, string>();
foreach (var c in expr.DescendantsAndSelf().OfType<ConstantExpression>())
{
if (c.Value is null) continue;
var t = c.Value.GetType();
if (t.IsPrimitive || t.IsEnum || t == typeof(string) || t == typeof(decimal) || t == typeof(DateTime))
continue;
var varName = $"captured{replacements.Count}";
replacements[c.Value] = varName;
}
translator.TranslateExpression(expr, replacements, namespaces, accessors); Prevention
- Pass a constantReplacements dictionary mapping every captured complex object to a variable name.
- Implement IRelationalQuotableExpression on custom types that must appear as quotable constants.
- Avoid capturing entity/service objects as constants; inject them as parameters.
When it happens
Trigger: A ConstantExpression holding a complex object (non-primitive, non-enum, non-tuple, non-null) that the translator cannot render as a C# literal.
Common situations: Expression trees capturing complex objects as constants; custom value types without quotable support; closures over entity instances or service objects.
Related errors
- Lifted expressions remaining at top-level in expression cont
- Got translated node of type '{Result?.GetType().Name ?? "<nu
- Lambda with null expression body
- Lambda with modifiers not supported: {lambda.Modifiers}
- Async lambdas are not supported
AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06).
Data as JSON: /api/errors/67ade43128a500ab.
Report an issue: GitHub.