{"record":{"id":"67ade43128a500ab","repo":"dotnet/efcore","slug":"encountered-a-constant-of-unsupported-type-value","errorCode":null,"errorMessage":"Encountered a constant of unsupported type '{value.GetType().Name}'. Only primitive constant nodes are supported.\n{value}","messagePattern":"Encountered a constant of unsupported type '(.+?)'\\. Only primitive constant nodes are supported\\.\n(.+?)","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Design/Query/Internal/LinqToCSharpSyntaxTranslator.cs","lineNumber":1188,"sourceCode":"        }\n    }\n\n    /// <summary>\n    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to\n    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in\n    ///     any release. You should only use it directly in your code with extreme caution and knowing that\n    ///     doing so can result in application failures when updating to a new Entity Framework Core release.\n    /// </summary>\n    protected virtual ExpressionSyntax GenerateUnknownValue(object value)\n    {\n        var type = value.GetType();\n        return type.IsValueType\n            && value.Equals(type.GetDefaultValue())\n                ? DefaultExpression(Generate(type))\n                : value is IRelationalQuotableExpression relationalQuotableExpression\n                && Translate(relationalQuotableExpression.Quote()) is ExpressionSyntax expressionSyntax\n                    ? expressionSyntax\n                    : throw new NotSupportedException(\n                        $\"Encountered a constant of unsupported type '{value.GetType().Name}'. Only primitive constant nodes are supported.\"\n                        + Environment.NewLine\n                        + value);\n    }\n\n    /// <inheritdoc />\n    protected override Expression VisitDebugInfo(DebugInfoExpression node)\n        => throw new NotSupportedException(\"DebugInfo nodes are not supporting when translating expression trees to C#\");\n\n    /// <inheritdoc />\n    protected override Expression VisitDefault(DefaultExpression node)\n    {\n        Result = DefaultExpression(Generate(node.Type));\n\n        return node;\n    }\n\n    /// <inheritdoc />","sourceCodeStart":1170,"sourceCodeEnd":1206,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Design/Query/Internal/LinqToCSharpSyntaxTranslator.cs#L1170-L1206","documentation":"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.","triggerScenarios":"A ConstantExpression holding a complex object (non-primitive, non-enum, non-tuple, non-null) that the translator cannot render as a C# literal.","commonSituations":"Expression trees capturing complex objects as constants; custom value types without quotable support; closures over entity instances or service objects.","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."],"exampleFix":"// before\nExpression.Constant(myComplexObject)\n// after\n// register a constant replacement so the translator emits the variable name:\n// constantReplacements[myComplexObject] = \"myComplexObject\"","handlingStrategy":"validation","validationCode":"// Pre-scan for non-primitive constants and register replacements\nvar replacements = new Dictionary<object, string>();\nforeach (var c in expr.DescendantsAndSelf().OfType<ConstantExpression>())\n{\n    if (c.Value is null) continue;\n    var t = c.Value.GetType();\n    if (t.IsPrimitive || t.IsEnum || t == typeof(string) || t == typeof(decimal) || t == typeof(DateTime))\n        continue;\n    var varName = $\"captured{replacements.Count}\";\n    replacements[c.Value] = varName;\n}\ntranslator.TranslateExpression(expr, replacements, namespaces, accessors);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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."],"tags":["efcore","expression-trees","constants","code-generation"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}