{"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/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Design/Query/Internal/CSharpToLinqTranslator.cs#L820-L856","documentation":"The constructor symbol resolved, but reflection type.GetConstructor(paramTypes) returned null and the type is not the struct parameterless-constructor case. The constructor is known to Roslyn but not found via reflection, so the translator cannot build the NewExpression with arguments.","triggerScenarios":"A non-public constructor, a constructor with by-ref/out/in parameters or custom modifiers whose reflection signature doesn't match, or a constructor defined in a different assembly version than the runtime load.","commonSituations":"Internal constructors across assemblies; constructors with in/ref parameters; records or types with non-public constructors; interop types.","solutions":["Make the constructor public","Add [InternalsVisibleTo] for the consuming assembly","Use the parameterless constructor plus property initializers instead","Avoid by-ref constructor parameters in precompiled queries"],"exampleFix":"// before\ninternal Entity(int id) { Id = id; }\n// after\npublic Entity() { }\n// usage: new Entity { Id = b.Id }","handlingStrategy":"validation","validationCode":"// Confirm the constructor is reflection-discoverable with the exact parameter types.\nvar ctor = typeof(Entity).GetConstructor(new[] { typeof(int) });\nif (ctor is null) { /* make public / add InternalsVisibleTo / drop by-ref params */ }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Make constructors used in precompiled queries public, or grant InternalsVisibleTo","Avoid ref/out/in constructor parameters","Use parameterless construction plus property initializers where possible"],"tags":["efcore","precompiled-queries","reflection","object-construction","csharp"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T02:17:10.218Z"}