{"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":"validation","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/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Design/Design/Internal/CSharpHelper.cs#L1254-L1290","documentation":"ArgumentException from CSharpHelper.Fragment when typeQualified is true: rendering a method call as a type-qualified fragment requires a non-null instanceIdentifier, a non-null DeclaringType, and no ChainedCall. If any of those is missing the helper refuses to produce output rather than emit invalid code.","triggerScenarios":"Calling Fragment with typeQualified:true on a method-call code fragment that has ChainedCall set, or passing instanceIdentifier null, or a fragment whose DeclaringType was not supplied by the provider's chain builder. Typically hit by provider/plugin authors composing API fluent chains.","commonSituations":"A provider extending the model fluent API via MethodCallCodeFragment with a chained call while the caller requests type-qualified output (e.g. generated DbContext.OnModelCreating using a static-style call).","solutions":["Pass a concrete instanceIdentifier when typeQualified is true.","Ensure the fragment has DeclaringType set (set via the constructor or a builder that captures the declaring type).","Drop the ChainedCall before requesting type-qualified output, or call Fragment with typeQualified:false for chained fragments."],"exampleFix":"// before\nvar frag = new MethodCallCodeFragment(\"MyMethod\", args) { ChainedCall = chained };\nvar code = helper.Fragment(frag, instanceIdentifier: null, typeQualified: true);\n\n// after\nvar code = helper.Fragment(frag with { ChainedCall = null }, instanceIdentifier: \"modelBuilder\", typeQualified: true);","handlingStrategy":"validation","validationCode":"if (typeQualified\n    && (instanceIdentifier is null || fragment.DeclaringType is null || fragment.ChainedCall is not null))\n{\n    throw new ArgumentException(\n        \"Type-qualified fragment needs instanceIdentifier, DeclaringType, and no chained call.\");\n}","typeGuard":"static bool CanBeTypeQualified(MethodCallCodeFragment f, string? id)\n    => id is not null && f.DeclaringType is not null && f.ChainedCall is null;\n","tryCatchPattern":null,"preventionTips":["When composing chained method-call fragments, request non-type-qualified output.","Always set DeclaringType when constructing MethodCallCodeFragment for providers.","Unit-test provider fragment generation against both typeQualified modes."],"tags":["scaffolding","design-time","method-call","code-generation"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}