dotnet/efcore · error · NotSupportedException

Anonymous type creation without members

Error message

Anonymous type creation without members

What it means

For anonymous-type instantiation the translator maps constructor arguments to property names using node.Members to render new { A = x, B = y }. If node.Members is null it has no names to emit, so VisitNew throws NotSupportedException. The C# compiler always supplies Members for anonymous news; a null Members only arises from low-level Expression.New overloads that omit them.

Source

Thrown at src/EFCore.Design/Query/Internal/LinqToCSharpSyntaxTranslator.cs:2110

        Result = _g.ArrayCreationExpression(elementType, expressions);

        return newArray;
    }

    /// <inheritdoc />
    protected override Expression VisitNew(NewExpression node)
    {
        using var _ = ChangeContext(ExpressionContext.Expression);

        var arguments = node.Constructor is null
            ? []
            : TranslateMethodArguments(node.Constructor.GetParameters(), node.Arguments);

        if (node.Type.IsAnonymousType())
        {
            if (node.Members is null)
            {
                throw new NotSupportedException("Anonymous type creation without members");
            }

            Result = AnonymousObjectCreationExpression(
                SeparatedList(
                    arguments.Select((arg, i) =>
                            AnonymousObjectMemberDeclarator(NameEquals(node.Members[i].Name), arg.Expression))
                        .ToArray()));

            return node;
        }

        // If the constructor isn't public, or it has required properties and the constructor doesn't have [SetsRequiredMembers], we can't
        // just generate a regular instantiation expression (won't compile). Generate an unsafe accessor instead.
        if (node.Constructor is { } constructor
            && (!constructor.IsPublic
                || (node.Type.GetCustomAttribute<RequiredMemberAttribute>() is not null
                    && constructor.GetCustomAttribute<SetsRequiredMembersAttribute>() is null)))
        {

View on GitHub (pinned to dbf9771522)

Solutions

  1. Use the Expression.New overload that supplies MemberInfo[] members for anonymous types (matching the constructor parameters to properties).
  2. Let the C# compiler build the anonymous new { ... } lambda instead of constructing it by hand.
  3. Use a named type so Members is irrelevant.

Example fix

// before
var newExpr = Expression.New(anonCtor, args); // Members is null -> throws
// after
var newExpr = Expression.New(anonCtor, args, anonProperties);
Defensive patterns

Strategy: validation

Validate before calling

protected override Expression VisitNew(NewExpression n) {
    if (n.Type.IsAnonymousType() && n.Members is null) Found = true;
    return n;
}

Prevention

When it happens

Trigger: A NewExpression whose Type.IsAnonymousType() is true but Members is null — produced by Expression.New(constructor, arguments) without the members overload, or by an expression rewriter that drops the Members collection.

Common situations: Programmatic construction of anonymous-type news via the lower-level Expression.New; tree rewriting that loses MemberInfo bindings.

Related errors


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