{"record":{"id":"7ab816353f46bc3e","repo":"dotnet/efcore","slug":"private-static-field-access","errorCode":null,"errorMessage":"Private static field access","messagePattern":"Private static field access","errorType":"exception","errorClass":"NotImplementedException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Design/Query/Internal/LinqToCSharpSyntaxTranslator.cs","lineNumber":1674,"sourceCode":"\n                Result = MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, expression, IdentifierName(member.Member.Name));\n                break;\n        }\n\n        return member;\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    protected virtual void TranslateNonPublicMemberAccess(MemberExpression memberExpression)\n    {\n        if (memberExpression.Expression is null)\n        {\n            throw new NotImplementedException(\"Private static field access\");\n        }\n\n        // Get an unsafe accessor for this field/property (this internally caches and adds it to the output list of unsafe accessors)\n\n        // [UnsafeAccessor(UnsafeAccessorKind.Field, Name = \"<Name>k__BackingField\")]\n        // static extern ref int UnsafeAccessor_Foo_Name(Foo f);\n        var unsafeAccessorDeclaration = GetUnsafeAccessorDeclaration(\n            memberExpression.Member is PropertyInfo propertyInfo\n                ? propertyInfo.GetMethod ?? throw new UnreachableException(\"Attempting to read from property without getter\")\n                : memberExpression.Member,\n            forWrite: false);\n\n        // The unsafe accessor declaration has been created; invoke it.\n        Result =\n            _g.InvocationExpression(\n                _g.IdentifierName(unsafeAccessorDeclaration.Identifier.Text),\n                Translate<ExpressionSyntax>(memberExpression.Expression));\n    }","sourceCodeStart":1656,"sourceCodeEnd":1692,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Design/Query/Internal/LinqToCSharpSyntaxTranslator.cs#L1656-L1692","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Expose the value via a public static member or method on the owning type.","Capture the value into a local/parameter before the query so it becomes a constant rather than a member access.","Make the static member public, or copy its value into a public surface."],"exampleFix":"// before\ninternal static class Cache { static readonly int Limit = 100; }\nvar q = ctx.Items.Where(i => i.Count < Cache.Limit); // private/internal static read\n// after\ninternal static class Cache { public static readonly int Limit = 100; }\n// or capture: var limit = Cache.Limit; var q = ctx.Items.Where(i => i.Count < limit);","handlingStrategy":"validation","validationCode":"// Flag non-public STATIC member reads\npublic sealed class StaticPrivateReadDetector : ExpressionVisitor {\n    public bool Found;\n    protected override Expression VisitMember(MemberExpression m) {\n        if (m.Expression is null && !IsPublic(m.Member)) Found = true;\n        return m;\n    }\n    static bool IsPublic(System.Reflection.MemberInfo mi)\n        => mi is System.Reflection.FieldInfo f ? f.IsPublic\n         : mi is System.Reflection.PropertyInfo p ? (p.GetMethod?.IsPublic ?? false)\n         : true;\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Capture static values into locals before the query so they become constants.","Expose static members publicly if they must be referenced directly.","Avoid referencing private/internal static state from compiled queries."],"tags":["ef-core","linq","expression-tree","unsafe-accessor","members"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T02:17:10.218Z"}