{"record":{"id":"4dc410ee54180a12","repo":"dotnet/efcore","slug":"can-t-translate-method-call-method-name-which","errorCode":null,"errorMessage":"Can't translate method '{call.Method.Name}' which has no declaring type","messagePattern":"Can't translate method '(.+?)' which has no declaring type","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Design/Query/Internal/LinqToCSharpSyntaxTranslator.cs","lineNumber":1930,"sourceCode":"        }\n\n        Result =\n            ElementAccessExpression(Translate<ExpressionSyntax>(index.Object!))\n                .WithArgumentList(\n                    BracketedArgumentList(\n                        SingletonSeparatedList(\n                            Argument(\n                                Translate<ExpressionSyntax>(index.Arguments.Single())))));\n\n        return index;\n    }\n\n    /// <inheritdoc />\n    protected override Expression VisitMethodCall(MethodCallExpression call)\n    {\n        if (call.Method.DeclaringType is null)\n        {\n            throw new NotSupportedException($\"Can't translate method '{call.Method.Name}' which has no declaring type\");\n        }\n\n        using var _ = ChangeContext(ExpressionContext.Expression);\n\n        var arguments = TranslateMethodArguments(call.Method.GetParameters(), call.Arguments);\n\n        // For generic methods, we check whether the generic type arguments are inferrable (e.g. they all appear in the parameters), and\n        // only explicitly specify the arguments if not. Note that this isn't just for prettier code: anonymous types cannot be explicitly\n        // named in code.\n        SimpleNameSyntax methodIdentifier;\n        if (!call.Method.IsGenericMethod || GenericTypeParameterAreInferrable())\n        {\n            methodIdentifier = IdentifierName(call.Method.Name);\n        }\n        else\n        {\n            Check.DebugAssert(\n                call.Method.GetGenericArguments().All(ga => !ga.IsAnonymousType()),","sourceCodeStart":1912,"sourceCodeEnd":1948,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Design/Query/Internal/LinqToCSharpSyntaxTranslator.cs#L1912-L1948","documentation":"VisitMethodCall requires call.Method.DeclaringType to be non-null because it must render a qualified reference (DeclaringType.MethodName(...)). A method with no declaring type cannot be turned into valid C#, so the translator throws NotSupportedException. Normal C# method calls always have a declaring type, so this signals a dynamically constructed or special-name method.","triggerScenarios":"A MethodCallExpression whose Method.DeclaringType is null — e.g. a reflection-built or dynamically assembled MethodInfo without an owning type, or certain global/special methods.","commonSituations":"Hand-constructed expression trees using DynamicMethod or reflection-built MethodInfos; rarely from normal compiled lambdas.","solutions":["Ensure the called MethodInfo belongs to a real type (call a normal instance or static method with a proper DeclaringType).","Avoid dynamic-method construction inside precompiled queries.","Wrap the operation in a helper method on a concrete type and call that instead."],"exampleFix":"// before: MethodInfo with null DeclaringType\nvar call = Expression.Call(methodInfoWithoutType, args);\n// after: call a normal method on a real type\nvar call = Expression.Call(typeof(Helper), nameof(Helper.Do), Type.EmptyTypes, args);","handlingStrategy":"try-catch","validationCode":"foreach (var call in MethodCallExtractor.From(lambda.Body)) {\n    if (call.Method.DeclaringType is null)\n        throw new InvalidOperationException($\"Method {call.Method.Name} has no declaring type.\");\n}","typeGuard":"static bool HasDeclaringType(MethodCallExpression c) => c.Method.DeclaringType is not null;","tryCatchPattern":"try { translator.Translate(lambda); }\ncatch (NotSupportedException ex) when (ex.Message.Contains(\"no declaring type\")) {\n    // rewrite the call onto a method with a real owning type, then retranslate\n}","preventionTips":["Always call methods that belong to a concrete type.","Avoid DynamicMethod / reflection-built MethodInfos in precompiled queries.","Wrap dynamic operations in a typed helper method."],"tags":["ef-core","linq","expression-tree","method-call","reflection"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}