dotnet/efcore · error · ArgumentException

A type-qualified method call requires an instance…

Error message

A type-qualified method call requires an instance identifier, a MethodInfo and no chained calls.

What it means

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.

Solutions

  1. Pass a concrete instanceIdentifier when typeQualified is true.
  2. Ensure the fragment has DeclaringType set (set via the constructor or a builder that captures the declaring type).
  3. Drop the ChainedCall before requesting type-qualified output, or call Fragment with typeQualified:false for chained fragments.

Example fix

// before
var frag = new MethodCallCodeFragment("MyMethod", args) { ChainedCall = chained };
var code = helper.Fragment(frag, instanceIdentifier: null, typeQualified: true);

// after
var code = helper.Fragment(frag with { ChainedCall = null }, instanceIdentifier: "modelBuilder", typeQualified: true);
Defensive patterns

Strategy: validation

Validate before calling

if (typeQualified
    && (instanceIdentifier is null || fragment.DeclaringType is null || fragment.ChainedCall is not null))
{
    throw new ArgumentException(
        "Type-qualified fragment needs instanceIdentifier, DeclaringType, and no chained call.");
}

Type guard

static bool CanBeTypeQualified(MethodCallCodeFragment f, string? id)
    => id is not null && f.DeclaringType is not null && f.ChainedCall is null;

Prevention

When it happens

Trigger: 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.

Common situations: 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).

Related errors


AI-assisted analysis of dotnet/efcore@3a2006ef56 (2026-08-11). Data as JSON: /api/errors/52787cca4c20e815. Report an issue: GitHub.

Appendix: source

Thrown at src/EFCore.Design/Design/Internal/CSharpHelper.cs:1272

        return true;
    }

    /// <summary>
    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
    ///     any release. You should only use it directly in your code with extreme caution and knowing that
    ///     doing so can result in application failures when updating to a new Entity Framework Core release.
    /// </summary>
    public virtual string Fragment(IMethodCallCodeFragment fragment, string? instanceIdentifier, bool typeQualified)
    {
        var builder = new StringBuilder();

        if (typeQualified)
        {
            if (instanceIdentifier is null || fragment.DeclaringType is null || fragment.ChainedCall is not null)
            {
                throw new ArgumentException(DesignStrings.CannotGenerateTypeQualifiedMethodCall);
            }

            builder
                .Append(fragment.DeclaringType)
                .Append('.')
                .Append(fragment.Method)
                .Append('(')
                .Append(instanceIdentifier);

            foreach (var argument in fragment.Arguments)
            {
                builder.Append(", ");

                if (argument is NestedClosureCodeFragment nestedFragment)
                {
                    builder.Append(Fragment(nestedFragment, 1));
                }
                else

View on GitHub (pinned to 3a2006ef56)