{"record":{"id":"2512f1147c0fea22","repo":"dotnet/efcore","slug":"private-static-field-assignment","errorCode":null,"errorMessage":"Private static field assignment","messagePattern":"Private static field assignment","errorType":"exception","errorClass":"NotImplementedException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Design/Query/Internal/LinqToCSharpSyntaxTranslator.cs","lineNumber":1708,"sourceCode":"                _g.IdentifierName(unsafeAccessorDeclaration.Identifier.Text),\n                Translate<ExpressionSyntax>(memberExpression.Expression));\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 TranslateNonPublicMemberAssignment(\n        MemberExpression memberExpression,\n        Expression value,\n        SyntaxKind assignmentKind)\n    {\n        // LINQ expression trees can directly access private members, but C# code cannot. Use the .NET [UnsafeAccessor] feature.\n        if (memberExpression.Expression is null)\n        {\n            throw new NotImplementedException(\"Private static field assignment\");\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.SetMethod ?? throw new UnreachableException(\"Attempting to assign to property without setter\")\n                : memberExpression.Member,\n            forWrite: true);\n\n        // The unsafe accessor declaration has been created; invoke it.\n        Result = memberExpression.Member switch\n        {\n            FieldInfo => AssignmentExpression(\n                assignmentKind,\n                (ExpressionSyntax)_g.InvocationExpression(","sourceCodeStart":1690,"sourceCodeEnd":1726,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Design/Query/Internal/LinqToCSharpSyntaxTranslator.cs#L1690-L1726","documentation":"TranslateNonPublicMemberAssignment is the write-side counterpart of the static-read guard: if memberExpression.Expression is null the assignment is to a static member, but the [UnsafeAccessor] setter generation requires an instance, so assigning a non-public static field or property throws NotImplementedException.","triggerScenarios":"A lambda assigns to a non-public static field or property, e.g. mutating an internal static counter or cache from within an expression EF Core precompiles.","commonSituations":"Expression trees that mutate static state; unusual for normal queries but possible in programmatically built update/init trees.","solutions":["Do not assign static state from within query expressions.","Expose a public static setter or method and call that instead.","Move the assignment out of the compiled tree into regular code."],"exampleFix":"// before\ninternal static class State { static int Count; }\nExpression.Assign(Expression.Field(null, typeof(State), \"Count\"), value);\n// after\ninternal static class State { public static int Count; }","handlingStrategy":"validation","validationCode":"// Flag assignment to non-public STATIC members\nprotected override Expression VisitBinary(BinaryExpression b) {\n    if (b.Left is MemberExpression m && m.Expression is null && !IsPublic(m.Member)) Found = true;\n    return b;\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Do not mutate static state from query expressions.","Use a public setter or method for static writes.","Move assignment logic out of the compiled tree."],"tags":["ef-core","linq","expression-tree","unsafe-accessor","members","assignment"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T02:17:10.218Z"}