{"record":{"id":"c942181484d0cd43","repo":"dotnet/efcore","slug":"cannot-scaffold-c-literals-of-type-literaltype","errorCode":null,"errorMessage":"Cannot scaffold C# literals of type '{literalType}'. The provider should implement CoreTypeMapping.GenerateCodeLiteral to support using it at design time.","messagePattern":"Cannot scaffold C# literals of type '(.+?)'\\. The provider should implement CoreTypeMapping\\.GenerateCodeLiteral to support using it at design time\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Design/Design/Internal/CSharpHelper.cs","lineNumber":1114,"sourceCode":"            }\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()!))\n                    .Append(\"[] { \");\n\n                HandleList(((NewArrayExpression)expression).Expressions, builder, simple: true);\n\n                builder\n                    .Append(\" }\");\n","sourceCodeStart":1096,"sourceCodeEnd":1132,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Design/Design/Internal/CSharpHelper.cs#L1096-L1132","documentation":"Thrown as InvalidOperationException by CSharpHelper.UnknownLiteral when a value's CLR type is not in the built-in LiteralFuncs table, is not an Enum/Type/Array/ValueTuple, is not a List<> or Dictionary<,>, AND no CoreTypeMapping could be found for it via ITypeMappingSource.FindMapping. The message points the provider author to implement CoreTypeMapping.GenerateCodeLiteral so design-time code generation can emit the literal.","triggerScenarios":"Scaffolding/migration encounters a value of a type that the active type-mapping source does not map at all (FindMapping returns null) and that CSharpHelper has no built-in literal writer for. Common with custom CLR types used in HasData or default values.","commonSituations":"Using a custom value type or third-party type (e.g., a specific NodaTime instant, a record struct) as a default/seed value without registering a type mapping and literal generator. Provider extension not installed at design time (missing the provider's design-time package). Switching providers where one mapped a type and the other does not.","solutions":["Register a CoreTypeMapping for the type in your provider/model configuration that implements GenerateCodeLiteral.","Avoid using unmapped CLR types as literal/seed/default values; convert them to a supported primitive first.","Ensure the correct EF Core provider (and its design-time package) is referenced by the startup project.","For HasData, project the seed into a simple anonymous/DTO type with mappable properties."],"exampleFix":"// before - custom Money struct used as a default value, no mapping\nmodelBuilder.Entity<Order>().Property(o => o.Amount).HasDefaultValue(new Money(10m));\n// throws: Cannot scaffold C# literals of type 'Money'\n\n// after - register a type mapping with GenerateCodeLiteral, or use a mappable primitive\nmodelBuilder.Entity<Order>().Property(o => o.Amount).HasDefaultValue(10m);","handlingStrategy":"validation","validationCode":"// Before scaffolding, ensure the value's type has a type mapping.\nvar mapping = typeMappingSource.FindMapping(value.GetType());\nif (mapping == null)\n    throw new InvalidOperationException($\"No type mapping for {value.GetType()}; cannot scaffold a literal.\");","typeGuard":null,"tryCatchPattern":"try { var literal = helper.UnknownLiteral(value); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"Cannot scaffold C# literals\"))\n{\n    // register a CoreTypeMapping with GenerateCodeLiteral, or use a mappable primitive\n}","preventionTips":["Register a type mapping (with GenerateCodeLiteral) for any custom type used in defaults/seeds.","Prefer mappable primitive types for HasData and HasDefaultValue.","Keep the correct provider and its design-time package referenced in the startup project."],"tags":["design","scaffolding","type-mapping","codegen"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}