{"record":{"id":"52787cca4c20e815","repo":"dotnet/efcore","slug":"a-type-qualified-method-call-requires-an-instance","errorCode":null,"errorMessage":"A type-qualified method call requires an instance identifier, a MethodInfo and no chained calls.","messagePattern":"A type-qualified method call requires an instance identifier, a MethodInfo and no chained calls\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Design/Design/Internal/CSharpHelper.cs","lineNumber":1272,"sourceCode":"\n        return true;\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 virtual string Fragment(IMethodCallCodeFragment fragment, string? instanceIdentifier, bool typeQualified)\n    {\n        var builder = new StringBuilder();\n\n        if (typeQualified)\n        {\n            if (instanceIdentifier is null || fragment.DeclaringType is null || fragment.ChainedCall is not null)\n            {\n                throw new ArgumentException(DesignStrings.CannotGenerateTypeQualifiedMethodCall);\n            }\n\n            builder\n                .Append(fragment.DeclaringType)\n                .Append('.')\n                .Append(fragment.Method)\n                .Append('(')\n                .Append(instanceIdentifier);\n\n            foreach (var argument in fragment.Arguments)\n            {\n                builder.Append(\", \");\n\n                if (argument is NestedClosureCodeFragment nestedFragment)\n                {\n                    builder.Append(Fragment(nestedFragment, 1));\n                }\n                else","sourceCodeStart":1254,"sourceCodeEnd":1290,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Design/Design/Internal/CSharpHelper.cs#L1254-L1290","documentation":"Thrown as ArgumentException by CSharpHelper.Fragment when typeQualified is true but the method-call fragment cannot be emitted as a type-qualified call. A type-qualified call (e.g., 'DbContextOptions.UseSqlite(x, ...)') requires three things: a non-null instanceIdentifier, a non-null fragment.DeclaringType, and no ChainedCall (chained fluent calls cannot be type-qualified). Violating any one rejects the request.","triggerScenarios":"Calling the internal Fragment(fragment, instanceIdentifier, typeQualified: true) with a null instance identifier, a fragment whose DeclaringType is null, or a fragment that has a ChainedCall. Reached during code generation that needs to emit a static-style method call referencing the instance as the first argument.","commonSituations":"A provider/plugin calling Fragment in type-qualified mode with a chained code fragment (which only makes sense for fluent 'builder.X().Y()' emissions). Passing an IMethodCallCodeFragment that did not set DeclaringType. Building custom migrations/annotation code generators that misuse the typeQualified path.","solutions":["Do not pass typeQualified: true for chained fragments; emit them as instance calls instead.","Ensure the fragment has a DeclaringType set and pass a non-null instanceIdentifier when using typeQualified mode.","If chaining is intentional, drop the typeQualified flag and use the instance-identifier overload."],"exampleFix":"// before - type-qualified call attempted on a chained fragment\nhelper.Fragment(chainedFragment, instanceIdentifier, typeQualified: true); // throws\n\n// after - use instance mode for chained calls\nhelper.Fragment(chainedFragment, instanceIdentifier, typeQualified: false);","handlingStrategy":"validation","validationCode":"// Only use typeQualified mode when all preconditions hold.\nif (fragment.DeclaringType is not null && instanceIdentifier is not null && fragment.ChainedCall is null)\n{\n    helper.Fragment(fragment, instanceIdentifier, typeQualified: true);\n}\nelse\n{\n    helper.Fragment(fragment, instanceIdentifier, typeQualified: false);\n}","typeGuard":null,"tryCatchPattern":"try { helper.Fragment(fragment, instanceIdentifier, typeQualified: true); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"type-qualified method call\"))\n{\n    helper.Fragment(fragment, instanceIdentifier, typeQualified: false);\n}","preventionTips":["Reserve typeQualified mode for single, non-chained method calls with a DeclaringType.","Do not attempt to type-qualify fluent chained fragments.","Ensure IMethodCallCodeFragment sets DeclaringType when type qualification is expected."],"tags":["design","codegen","method-call","provider"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}