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

  1. Provide a constantReplacements map that substitutes the object for a variable name declared in the surrounding generated method.
  2. Implement IRelationalQuotableExpression on the type so it can be quoted into an expression.
  3. 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

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


AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06). Data as JSON: /api/errors/67ade43128a500ab. Report an issue: GitHub.