dotnet/efcore · error · NotSupportedException

The literal expression '{expression}' for '{type}' cannot be

Error message

The literal expression '{expression}' for '{type}' cannot be parsed. Only simple constructor calls and factory methods are supported.

What it means

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.

Source

Thrown at src/EFCore.Design/Design/Internal/CSharpHelper.cs:1107

            var genericArguments = valueType.GetGenericArguments();
            switch (value)
            {
                case IList list when genericArguments.Length == 1 && valueType.GetGenericTypeDefinition() == typeof(List<>):
                    return List(genericArguments[0], list);
                case IDictionary dict when genericArguments.Length == 2 && valueType.GetGenericTypeDefinition() == typeof(Dictionary<,>):
                    return Dictionary(genericArguments[0], genericArguments[1], dict);
            }
        }

        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()!))

View on GitHub (pinned to dbf9771522)

Solutions

  1. Override CoreTypeMapping.GenerateCodeLiteral in the type mapping to return a simple 'new T(...)' or static factory method call expression.
  2. Simplify the value being scaffolded (avoid complex default values that cannot be expressed as a single constructor/factory call).
  3. If you control the value, add a dedicated factory method and have GenerateCodeLiteral return a call to it.
  4. Report the unsupported expression to the provider author if it is a built-in provider limitation.

Example fix

// before - GenerateCodeLiteral returns an expression HandleExpression can't parse
protected override Expression GenerateCodeLiteral(object value)
    => Expression.Condition(...); // NotSupportedException

// after - use a simple constructor or factory call
protected override Expression GenerateCodeLiteral(object value)
    => Expression.New(typeof(MyType).GetConstructor([typeof(int)])!, Expression.Constant(42));
Defensive patterns

Strategy: try-catch

Try / catch

try { var literal = helper.UnknownLiteral(value); }
catch (NotSupportedException ex) when (ex.Message.Contains("cannot be parsed"))
{
    // simplify the value or override GenerateCodeLiteral to return a simple New/Call expression
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06). Data as JSON: /api/errors/ab2ca9c4601d50aa. Report an issue: GitHub.