dotnet/efcore · error · NotImplementedException

Private static field assignment

Error message

Private static field assignment

What it means

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.

Source

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

                _g.IdentifierName(unsafeAccessorDeclaration.Identifier.Text),
                Translate<ExpressionSyntax>(memberExpression.Expression));
    }

    /// <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 TranslateNonPublicMemberAssignment(
        MemberExpression memberExpression,
        Expression value,
        SyntaxKind assignmentKind)
    {
        // LINQ expression trees can directly access private members, but C# code cannot. Use the .NET [UnsafeAccessor] feature.
        if (memberExpression.Expression is null)
        {
            throw new NotImplementedException("Private static field assignment");
        }

        // 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.SetMethod ?? throw new UnreachableException("Attempting to assign to property without setter")
                : memberExpression.Member,
            forWrite: true);

        // The unsafe accessor declaration has been created; invoke it.
        Result = memberExpression.Member switch
        {
            FieldInfo => AssignmentExpression(
                assignmentKind,
                (ExpressionSyntax)_g.InvocationExpression(

View on GitHub (pinned to dbf9771522)

Solutions

  1. Do not assign static state from within query expressions.
  2. Expose a public static setter or method and call that instead.
  3. Move the assignment out of the compiled tree into regular code.

Example fix

// before
internal static class State { static int Count; }
Expression.Assign(Expression.Field(null, typeof(State), "Count"), value);
// after
internal static class State { public static int Count; }
Defensive patterns

Strategy: validation

Validate before calling

// Flag assignment to non-public STATIC members
protected override Expression VisitBinary(BinaryExpression b) {
    if (b.Left is MemberExpression m && m.Expression is null && !IsPublic(m.Member)) Found = true;
    return b;
}

Prevention

When it happens

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

Common situations: Expression trees that mutate static state; unusual for normal queries but possible in programmatically built update/init trees.

Related errors


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