dotnet/efcore · error · InvalidOperationException
Cannot scaffold C# literals of type '{literalType}'. The pro
Error message
Cannot scaffold C# literals of type '{literalType}'. The provider should implement CoreTypeMapping.GenerateCodeLiteral to support using it at design time. What it means
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.
Source
Thrown at src/EFCore.Design/Design/Internal/CSharpHelper.cs:1114
}
}
var mapping = _typeMappingSource.FindMapping(literalType);
if (mapping != null)
{
var builder = new StringBuilder();
var expression = mapping.GenerateCodeLiteral(value);
var handled = HandleExpression(expression, builder);
return !handled
? throw new NotSupportedException(
DesignStrings.LiteralExpressionNotSupported(
expression.ToString(),
literalType.ShortDisplayName()))
: builder.ToString();
}
throw new InvalidOperationException(DesignStrings.UnknownLiteral(literalType));
}
private bool HandleExpression(Expression expression, StringBuilder builder, bool simple = false)
{
// Only handle trivially simple cases for `new` and factory methods
switch (expression.NodeType)
{
case ExpressionType.NewArrayInit:
builder
.Append("new ")
.Append(Reference(expression.Type.GetElementType()!))
.Append("[] { ");
HandleList(((NewArrayExpression)expression).Expressions, builder, simple: true);
builder
.Append(" }");
View on GitHub (pinned to dbf9771522)
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.
Example fix
// before - custom Money struct used as a default value, no mapping modelBuilder.Entity<Order>().Property(o => o.Amount).HasDefaultValue(new Money(10m)); // throws: Cannot scaffold C# literals of type 'Money' // after - register a type mapping with GenerateCodeLiteral, or use a mappable primitive modelBuilder.Entity<Order>().Property(o => o.Amount).HasDefaultValue(10m);
Defensive patterns
Strategy: validation
Validate before calling
// Before scaffolding, ensure the value's type has a type mapping.
var mapping = typeMappingSource.FindMapping(value.GetType());
if (mapping == null)
throw new InvalidOperationException($"No type mapping for {value.GetType()}; cannot scaffold a literal."); Try / catch
try { var literal = helper.UnknownLiteral(value); }
catch (InvalidOperationException ex) when (ex.Message.Contains("Cannot scaffold C# literals"))
{
// register a CoreTypeMapping with GenerateCodeLiteral, or use a mappable primitive
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- The literal expression '{expression}' for '{type}' cannot be
- A type-qualified method call requires an instance identifier
- Query precompilation failed with errors:
- The partition key value is of type '{valueType}' which is no
- The expression '{sqlExpression}' in the SQL tree does not ha
AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06).
Data as JSON: /api/errors/c942181484d0cd43.
Report an issue: GitHub.