dotnet/efcore · error · InvalidOperationException

ObjectCreation: couldn't find IMethodSymbol for constructor

Error message

ObjectCreation: couldn't find IMethodSymbol for constructor: {objectCreation}

What it means

Thrown by VisitObjectCreationExpression when Roslyn's GetSymbolInfo on an object creation expression returns a null or non-IMethodSymbol. The translator needs the constructor's IMethodSymbol to determine the type, parameter types, and find the matching reflection constructor. Without it, it cannot build a NewExpression and throws InvalidOperationException.

Solutions

  1. Ensure the project compiles cleanly — run dotnet build and fix any errors before precompilation.
  2. If the type is in an external assembly, pass it via additionalAssembly to CSharpToLinqTranslator.Load().
  3. Avoid creating objects with dynamic-typed arguments — cast arguments to their concrete types.
  4. Verify the constructor you're calling is accessible (public or InternalsVisibleTo) to the precompilation assembly.
  5. If creating objects of types defined in the same solution, ensure all projects are built first.

Example fix

// before — type from un-referenced assembly
var query = ctx.Blogs.Select(b => new ExternalDto { Name = b.Name });

// after — ensure assembly is referenced and passed, or use a known type
var query = ctx.Blogs.Select(b => new BlogDto { Name = b.Name });
Defensive patterns

Strategy: validation

Validate before calling

// Before precompiling, verify all object creation expressions resolve to constructor symbols
var unresolvedCtors = querySyntaxTree.DescendantNodes()
    .OfType<ObjectCreationExpressionSyntax>()
    .Where(o => semanticModel.GetSymbolInfo(o).Symbol is not IMethodSymbol);
if (unresolvedCtors.Any())
    ReportError("Unresolved constructor in precompiled query — check type references");

Prevention

When it happens

Trigger: A precompiled query contains an object creation expression (`new SomeType(...)`) that Roslyn cannot resolve to a constructor symbol. Causes include: the type doesn't exist in the compilation (compile error), the constructor is ambiguous, the type is dynamic, or the semantic model has stale/incomplete information about the type's constructors.

Common situations: Creating objects of types defined in not-yet-compiled projects; creating objects whose constructors require assemblies not referenced by the precompilation; using `new` with dynamic-typed arguments where Roslyn can't resolve the overload; creating objects of types defined in assemblies loaded via additionalAssembly that wasn't passed; stale compilation after code edits.

Related errors


AI-assisted analysis of dotnet/efcore@3a2006ef56 (2026-08-11). Data as JSON: /api/errors/67c49697e04a3be2. Report an issue: GitHub.

Appendix: source

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

            ? expression.Type.GetArrayRank() != 1
                ? throw new NotImplementedException("MemberAccess on multi-dimensional array")
                : (Expression)ArrayLength(expression)
            : MakeMemberAccess(
                expression is ConstantExpression { Value: Type } ? null : expression,
                memberInfo);
    }

    /// <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 VisitObjectCreationExpression(ObjectCreationExpressionSyntax objectCreation)
    {
        if (_semanticModel.GetSymbolInfo(objectCreation).Symbol is not IMethodSymbol constructorSymbol)
        {
            throw new InvalidOperationException($"ObjectCreation: couldn't find IMethodSymbol for constructor: {objectCreation}");
        }

        Check.DebugAssert(constructorSymbol.MethodKind == MethodKind.Constructor);

        var type = ResolveType(constructorSymbol.ContainingType);

        // Find the reflection constructor that matches the constructor symbol's signature
        var parameterTypes = constructorSymbol.Parameters.Select(ps => ResolveType(ps.Type)).ToArray();
        var constructor = type.GetConstructor(parameterTypes);

        var newExpression = constructor is not null
            ? New(
                constructor,
                objectCreation.ArgumentList?.Arguments.Select(a => Visit(a)) ?? [])
            : parameterTypes.Length == 0 // For structs, there's no actual parameterless constructor
                ? New(type)
                : throw new InvalidOperationException($"ObjectCreation: Missing constructor: {objectCreation}");

View on GitHub (pinned to 3a2006ef56)