{"record":{"id":"67c49697e04a3be2","repo":"dotnet/efcore","slug":"objectcreation-couldn-t-find-imethodsymbol-for-co","errorCode":null,"errorMessage":"ObjectCreation: couldn't find IMethodSymbol for constructor: {objectCreation}","messagePattern":"ObjectCreation: couldn't find IMethodSymbol for constructor: (.+?)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Design/Query/Internal/CSharpToLinqTranslator.cs","lineNumber":821,"sourceCode":"            ? expression.Type.GetArrayRank() != 1\n                ? throw new NotImplementedException(\"MemberAccess on multi-dimensional array\")\n                : (Expression)ArrayLength(expression)\n            : MakeMemberAccess(\n                expression is ConstantExpression { Value: Type } ? null : expression,\n                memberInfo);\n    }\n\n    /// <summary>\n    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to\n    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in\n    ///     any release. You should only use it directly in your code with extreme caution and knowing that\n    ///     doing so can result in application failures when updating to a new Entity Framework Core release.\n    /// </summary>\n    public override Expression VisitObjectCreationExpression(ObjectCreationExpressionSyntax objectCreation)\n    {\n        if (_semanticModel.GetSymbolInfo(objectCreation).Symbol is not IMethodSymbol constructorSymbol)\n        {\n            throw new InvalidOperationException($\"ObjectCreation: couldn't find IMethodSymbol for constructor: {objectCreation}\");\n        }\n\n        Check.DebugAssert(constructorSymbol.MethodKind == MethodKind.Constructor);\n\n        var type = ResolveType(constructorSymbol.ContainingType);\n\n        // Find the reflection constructor that matches the constructor symbol's signature\n        var parameterTypes = constructorSymbol.Parameters.Select(ps => ResolveType(ps.Type)).ToArray();\n        var constructor = type.GetConstructor(parameterTypes);\n\n        var newExpression = constructor is not null\n            ? New(\n                constructor,\n                objectCreation.ArgumentList?.Arguments.Select(a => Visit(a)) ?? [])\n            : parameterTypes.Length == 0 // For structs, there's no actual parameterless constructor\n                ? New(type)\n                : throw new InvalidOperationException($\"ObjectCreation: Missing constructor: {objectCreation}\");\n","sourceCodeStart":803,"sourceCodeEnd":839,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Design/Query/Internal/CSharpToLinqTranslator.cs#L803-L839","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Ensure the project compiles cleanly — run dotnet build and fix any errors before precompilation.","If the type is in an external assembly, pass it via additionalAssembly to CSharpToLinqTranslator.Load().","Avoid creating objects with dynamic-typed arguments — cast arguments to their concrete types.","Verify the constructor you're calling is accessible (public or InternalsVisibleTo) to the precompilation assembly.","If creating objects of types defined in the same solution, ensure all projects are built first."],"exampleFix":"// before — type from un-referenced assembly\nvar query = ctx.Blogs.Select(b => new ExternalDto { Name = b.Name });\n\n// after — ensure assembly is referenced and passed, or use a known type\nvar query = ctx.Blogs.Select(b => new BlogDto { Name = b.Name });","handlingStrategy":"validation","validationCode":"// Before precompiling, verify all object creation expressions resolve to constructor symbols\nvar unresolvedCtors = querySyntaxTree.DescendantNodes()\n    .OfType<ObjectCreationExpressionSyntax>()\n    .Where(o => semanticModel.GetSymbolInfo(o).Symbol is not IMethodSymbol);\nif (unresolvedCtors.Any())\n    ReportError(\"Unresolved constructor in precompiled query — check type references\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Ensure the project compiles cleanly before precompilation.","Pass external assemblies containing constructed types via additionalAssembly.","Avoid creating objects with dynamic-typed arguments.","Ensure all referenced projects are built before running precompilation."],"tags":["precompiled-queries","roslyn","symbol-resolution","object-creation","constructor","expression-tree"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}