{"record":{"id":"599127f882da6630","repo":"dotnet/efcore","slug":"objectcreation-missing-constructor-objectcreati","errorCode":null,"errorMessage":"ObjectCreation: Missing constructor: {objectCreation}","messagePattern":"ObjectCreation: Missing constructor: (.+?)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Design/Query/Internal/CSharpToLinqTranslator.cs","lineNumber":838,"sourceCode":"        {\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\n        switch (objectCreation.Initializer)\n        {\n            // No initializers, just return the NewExpression\n            case null or { Expressions: [] }:\n                return newExpression;\n\n            // Assignment initializer (new Blog { Name = \"foo\" })\n            case { Expressions: [AssignmentExpressionSyntax, ..] }:\n                return MemberInit(\n                    newExpression,\n                    objectCreation.Initializer.Expressions.Select(e =>\n                    {\n                        if (e is not AssignmentExpressionSyntax { Left: var lValue, Right: var value })\n                        {\n                            throw new NotSupportedException(\n                                $\"ObjectCreation: non-assignment initializer expression of type '{e.GetType().Name}': {objectCreation}\");\n                        }","sourceCodeStart":820,"sourceCodeEnd":856,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Design/Query/Internal/CSharpToLinqTranslator.cs#L820-L856","documentation":"Thrown when Roslyn resolved the constructor symbol but Type.GetConstructor(parameterTypes) returned null AND the constructor is not a parameterless struct constructor. The translator tries to find the reflection constructor matching the symbol's parameter types; if it can't find it, it falls back to New(type) for parameterless struct constructors. If neither path works, it throws InvalidOperationException.","triggerScenarios":"A precompiled query creates an object where: the constructor's parameter types as resolved by ResolveType don't match the actual CLR constructor's parameter types (type-forwarding, version skew), the constructor is private/protected and inaccessible via default GetConstructor, or the constructor has ref/out parameters that change the type signature. The parameterless fallback only applies to structs.","commonSituations":"Creating objects whose constructors accept interfaces or abstract types where ResolveType maps differently; version skew between the referenced package and loaded assembly changing constructor signatures; creating objects with constructors that have ref/out parameters; creating objects of types with only internal constructors without InternalsVisibleTo.","solutions":["Verify assembly version consistency between compile-time and precompilation-time references.","If the constructor is internal, add InternalsVisibleTo for the precompilation assembly or make the constructor public.","Avoid constructors with ref/out parameters in precompiled queries.","Check that ResolveType maps the constructor's parameter types correctly — investigate by examining the Roslyn parameter types vs. the loaded assembly's constructor parameter types.","If creating a struct, ensure it has a usable parameterless constructor or provide a constructor that reflection can find."],"exampleFix":"// before — constructor with ref param, reflection can't find it\nvar query = ctx.Items.Select(i => new Wrapper(ref i.Data));\n\n// after — use a constructor with standard value params\nvar query = ctx.Items.Select(i => new Wrapper(i.Data));","handlingStrategy":"validation","validationCode":"// Before precompiling, verify constructors are findable via reflection\nforeach (var objCreation in querySyntaxTree.DescendantNodes().OfType<ObjectCreationExpressionSyntax>())\n{\n    if (semanticModel.GetSymbolInfo(objCreation).Symbol is IMethodSymbol ctor)\n    {\n        var type = ResolveTypeForCheck(ctor.ContainingType);\n        var paramTypes = ctor.Parameters.Select(p => ResolveTypeForCheck(p.Type)).ToArray();\n        var found = type.GetConstructor(paramTypes);\n        if (found is null && paramTypes.Length > 0)\n            ReportError($\"Constructor for {type.Name} not found via reflection\");\n    }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Avoid constructors with ref/out parameters in precompiled queries.","Keep package versions consistent between compile-time and precompile-time.","Make internal constructors public or add InternalsVisibleTo.","For structs, prefer the parameterless constructor or ensure the parameterized constructor is accessible.","Avoid constructing objects whose parameter types are interfaces or abstract types that may resolve differently."],"tags":["precompiled-queries","roslyn","reflection","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"}