{"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/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Design/Design/Internal/CSharpHelper.cs#L1096-L1132","documentation":"Thrown by CSharpHelper.UnknownLiteral when no CoreTypeMapping can be found for the literal type at all — FindMapping returns null, so there is nothing capable of producing code for the value. InvalidOperationException, surfaced while generating literals for migrations, seeds, or the compiled model.","triggerScenarios":"Using a value type as a constant/default/seed for which neither EF Core nor the active provider registers a type mapping. Common with clr types the provider does not natively map (e.g. Half, DateOnly on older providers, custom structs without a converter).","commonSituations":"Upgrading the database provider to a version that no longer maps a type, switching providers (SQL Server -> PostgreSQL) where a previously-mapped type is unsupported, or introducing a custom value object as a column default without a converter.","solutions":["Register a value converter for the property so it maps to a primitive the provider understands.","Avoid using the un-mapped type as HasDefaultValue/seed data; compute it at runtime instead.","Update the provider package to a version that ships a mapping for the type.","Implement and register an ITypeMappingSource plugin that returns a mapping for the literal type."],"exampleFix":"// before\nmodelBuilder.Entity<T>().Property(x => x.Custom).HasDefaultValue(new CustomStruct());\n\n// after - convert to a mapped primitive and apply default at runtime\nmodelBuilder.Entity<T>()\n    .Property(x => x.Custom)\n    .HasConversion(v => v.Serialize(), s => CustomStruct.Deserialize(s));","handlingStrategy":"validation","validationCode":"var mapping = typeMappingSource.FindMapping(literalType);\nif (mapping is null)\n{\n    throw new InvalidOperationException(\n        $\"No type mapping for {literalType}; register a value converter or pick a mapped type.\");\n}","typeGuard":"static bool TypeIsMapped(ITypeMappingSource src, Type t)\n    => src.FindMapping(t) is not null;\n","tryCatchPattern":"try { helper.UnknownLiteral(value); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"Cannot scaffold\"))\n{ /* register a converter or remove the literal */ }","preventionTips":["Register value converters for custom value objects used in the model.","Keep provider packages updated so mappings cover newer CLR types.","Do not use unmapped CLR types as HasDefaultValue or seed values."],"tags":["scaffolding","design-time","type-mapping","no-mapping"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}