{"record":{"id":"ab2ca9c4601d50aa","repo":"dotnet/efcore","slug":"the-literal-expression-expression-for-type","errorCode":null,"errorMessage":"The literal expression '{expression}' for '{type}' cannot be parsed. Only simple constructor calls and factory methods are supported.","messagePattern":"The literal expression '(.+?)' for '(.+?)' cannot be parsed\\. Only simple constructor calls and factory methods are supported\\.","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Design/Design/Internal/CSharpHelper.cs","lineNumber":1107,"sourceCode":"            var genericArguments = valueType.GetGenericArguments();\n            switch (value)\n            {\n                case IList list when genericArguments.Length == 1 && valueType.GetGenericTypeDefinition() == typeof(List<>):\n                    return List(genericArguments[0], list);\n                case IDictionary dict when genericArguments.Length == 2 && valueType.GetGenericTypeDefinition() == typeof(Dictionary<,>):\n                    return Dictionary(genericArguments[0], genericArguments[1], dict);\n            }\n        }\n\n        var mapping = _typeMappingSource.FindMapping(literalType);\n        if (mapping != null)\n        {\n            var builder = new StringBuilder();\n            var expression = mapping.GenerateCodeLiteral(value);\n            var handled = HandleExpression(expression, builder);\n\n            return !handled\n                ? throw new NotSupportedException(\n                    DesignStrings.LiteralExpressionNotSupported(\n                        expression.ToString(),\n                        literalType.ShortDisplayName()))\n                : builder.ToString();\n        }\n\n        throw new InvalidOperationException(DesignStrings.UnknownLiteral(literalType));\n    }\n\n    private bool HandleExpression(Expression expression, StringBuilder builder, bool simple = false)\n    {\n        // Only handle trivially simple cases for `new` and factory methods\n        switch (expression.NodeType)\n        {\n            case ExpressionType.NewArrayInit:\n                builder\n                    .Append(\"new \")\n                    .Append(Reference(expression.Type.GetElementType()!))","sourceCodeStart":1089,"sourceCodeEnd":1125,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Design/Design/Internal/CSharpHelper.cs#L1089-L1125","documentation":"Thrown as NotSupportedException by CSharpHelper.UnknownLiteral when a type mapping's GenerateCodeLiteral returns an expression tree that HandleExpression cannot reduce to a simple constructor call or static/instance factory method. Only New, Call, Constant, MemberAccess, Convert, NewArrayInit, and Add nodes are supported; anything else (complex logic, lambda bodies, unsupported method shapes) is rejected because the scaffolder cannot emit valid C# for it.","triggerScenarios":"During migrations/scaffolding, generating a code literal for a value whose CoreTypeMapping.GenerateCodeLiteral produces an expression tree with an unsupported node type (e.g., a conditional, invocation, or nested closure). Triggered when a custom value-comparer or type-mapping emits a non-trivial expression for a default value or seeded value.","commonSituations":"Using a third-party or custom EF Core provider whose type mapping does not implement GenerateCodeLiteral with a simple expression. Adding a complex default value or HasData seed whose CLR type requires a non-trivial construction expression. Upgrading the provider where the literal-generation contract tightened.","solutions":["Override CoreTypeMapping.GenerateCodeLiteral in the type mapping to return a simple 'new T(...)' or static factory method call expression.","Simplify the value being scaffolded (avoid complex default values that cannot be expressed as a single constructor/factory call).","If you control the value, add a dedicated factory method and have GenerateCodeLiteral return a call to it.","Report the unsupported expression to the provider author if it is a built-in provider limitation."],"exampleFix":"// before - GenerateCodeLiteral returns an expression HandleExpression can't parse\nprotected override Expression GenerateCodeLiteral(object value)\n    => Expression.Condition(...); // NotSupportedException\n\n// after - use a simple constructor or factory call\nprotected override Expression GenerateCodeLiteral(object value)\n    => Expression.New(typeof(MyType).GetConstructor([typeof(int)])!, Expression.Constant(42));","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { var literal = helper.UnknownLiteral(value); }\ncatch (NotSupportedException ex) when (ex.Message.Contains(\"cannot be parsed\"))\n{\n    // simplify the value or override GenerateCodeLiteral to return a simple New/Call expression\n}","preventionTips":["Override CoreTypeMapping.GenerateCodeLiteral to return only New, Call, Constant, MemberAccess, Convert, NewArrayInit, or Add expressions.","Avoid complex default/seed values that cannot be expressed as a single constructor or factory call.","Add a dedicated static factory method for the type and reference it from GenerateCodeLiteral."],"tags":["design","scaffolding","type-mapping","codegen"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}