dotnet/efcore · error · NotImplementedException

Private static field access

Error message

Private static field access

What it means

TranslateNonPublicMemberAccess throws NotImplementedException when memberExpression.Expression is null — the signature that the member access is static. The [UnsafeAccessor]-based path only handles non-public instance members (it needs an instance to pass to the accessor), so reading a private/internal/protected static field or property is unsupported.

Source

Thrown at src/EFCore.Design/Query/Internal/LinqToCSharpSyntaxTranslator.cs:1674

                Result = MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, expression, IdentifierName(member.Member.Name));
                break;
        }

        return member;
    }

    /// <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>
    protected virtual void TranslateNonPublicMemberAccess(MemberExpression memberExpression)
    {
        if (memberExpression.Expression is null)
        {
            throw new NotImplementedException("Private static field access");
        }

        // Get an unsafe accessor for this field/property (this internally caches and adds it to the output list of unsafe accessors)

        // [UnsafeAccessor(UnsafeAccessorKind.Field, Name = "<Name>k__BackingField")]
        // static extern ref int UnsafeAccessor_Foo_Name(Foo f);
        var unsafeAccessorDeclaration = GetUnsafeAccessorDeclaration(
            memberExpression.Member is PropertyInfo propertyInfo
                ? propertyInfo.GetMethod ?? throw new UnreachableException("Attempting to read from property without getter")
                : memberExpression.Member,
            forWrite: false);

        // The unsafe accessor declaration has been created; invoke it.
        Result =
            _g.InvocationExpression(
                _g.IdentifierName(unsafeAccessorDeclaration.Identifier.Text),
                Translate<ExpressionSyntax>(memberExpression.Expression));
    }

View on GitHub (pinned to dbf9771522)

Solutions

  1. Expose the value via a public static member or method on the owning type.
  2. Capture the value into a local/parameter before the query so it becomes a constant rather than a member access.
  3. Make the static member public, or copy its value into a public surface.

Example fix

// before
internal static class Cache { static readonly int Limit = 100; }
var q = ctx.Items.Where(i => i.Count < Cache.Limit); // private/internal static read
// after
internal static class Cache { public static readonly int Limit = 100; }
// or capture: var limit = Cache.Limit; var q = ctx.Items.Where(i => i.Count < limit);
Defensive patterns

Strategy: validation

Validate before calling

// Flag non-public STATIC member reads
public sealed class StaticPrivateReadDetector : ExpressionVisitor {
    public bool Found;
    protected override Expression VisitMember(MemberExpression m) {
        if (m.Expression is null && !IsPublic(m.Member)) Found = true;
        return m;
    }
    static bool IsPublic(System.Reflection.MemberInfo mi)
        => mi is System.Reflection.FieldInfo f ? f.IsPublic
         : mi is System.Reflection.PropertyInfo p ? (p.GetMethod?.IsPublic ?? false)
         : true;
}

Prevention

When it happens

Trigger: A precompiled query or model-building lambda reads a non-public static field or property, e.g. accessing an internal static lookup table or a private static constant of a type.

Common situations: Closing over or referencing private/internal static state from a compiled query; reading a static member of an internal type that EF Core must precompile.

Related errors


AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06). Data as JSON: /api/errors/7ab816353f46bc3e. Report an issue: GitHub.