dotnet/efcore · error · InvalidOperationException

Couldn't find single Add method on type '{type.Name}', requi

Error message

Couldn't find single Add method on type '{type.Name}', required for list initializer

What it means

For a collection initializer (new List<T> { a, b }) the translator looks for exactly one single-parameter Add method via SingleOrDefault. If no single-arg Add exists, or if multiple exist (which makes SingleOrDefault itself throw), the initializer cannot be translated to a ListInit.

Source

Thrown at src/EFCore.Design/Query/Internal/CSharpToLinqTranslator.cs:882

                            _ => throw new InvalidOperationException(
                                $"ObjectCreation: unsupported initializer for member of type '{lValueSymbol?.GetType().Name}': {e}")
                        };

                        return memberInfo is null
                            ? throw new InvalidOperationException(
                                $"ObjectCreation: couldn't find initialized member '{lValueSymbol.Name}': {e}")
                            : Bind(memberInfo, Visit(value));
                    }));

            // Non-assignment initializer => list initializer (new List<int> { 1, 2, 3 })
            default:
                // Find the correct Add() method on the collection type
                // TODO: This doesn't work if there are multiple Add() methods (contrived). Complete solution would be to find the base
                // TODO: type for all initializer expressions and find an Add overload of that type (or a superclass thereof)
                var addMethod = type.GetMethods().SingleOrDefault(m => m.Name == "Add" && m.GetParameters().Length == 1);
                if (addMethod is null)
                {
                    throw new InvalidOperationException(
                        $"Couldn't find single Add method on type '{type.Name}', required for list initializer");
                }

                // TODO: Dictionary initializer, where each ElementInit has more than one expression

                return ListInit(
                    newExpression,
                    objectCreation.Initializer.Expressions.Select(e => ElementInit(addMethod, Visit(e))));
        }
    }

    /// <summary>
    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
    ///     any release. You should only use it directly in your code with extreme caution and knowing that
    ///     doing so can result in application failures when updating to a new Entity Framework Core release.
    /// </summary>
    public override Expression VisitParenthesizedExpression(ParenthesizedExpressionSyntax parenthesized)

View on GitHub (pinned to dbf9771522)

Solutions

  1. For dictionaries, construct empty and add entries outside the query, or use index-initializer syntax where supported
  2. Avoid collection initializers on types with multiple single-arg Add overloads
  3. Use a concrete List<T> for the initializer
  4. Hoist the collection construction out of the precompiled query

Example fix

// before
var map = new Dictionary<int, string> { { 1, "a" }, { 2, "b" } };
// after (built outside the query)
var map = new Dictionary<int, string>();
map.Add(1, "a"); map.Add(2, "b");
Defensive patterns

Strategy: validation

Validate before calling

// Confirm the collection type has exactly one single-arg Add before using a collection initializer.
var adds = typeof(MyCollection).GetMethods().Where(m => m.Name == "Add" && m.GetParameters().Length == 1).ToArray();
if (adds.Length != 1) { /* build the collection outside the query */ }

Prevention

When it happens

Trigger: A collection initializer on a type with no single-argument Add (e.g. dictionaries needing Add(key, value), or ConcurrentDictionary which exposes TryAdd not Add), or on a custom collection with several single-arg Add overloads.

Common situations: Dictionary initializers new Dictionary<K,V> { { k, v } }; custom collections with multiple Add(T) overloads; initializing ConcurrentDictionary or other collections without a unique Add.

Related errors


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