dotnet/efcore · error · InvalidOperationException

No backing field was found for property '{1_entityType}.{0_p

Error message

No backing field was found for property '{1_entityType}.{0_property}'. Name the backing field so that it is discovered by convention, configure the backing field to use, or use a different '{propertyAccessMode}'.

What it means

Thrown by RegisterPrivateAccessor when property.TryGetMemberInfo(...) returns false, meaning EF cannot resolve a usable backing member (field or property accessor) for the property under the requested access mode. The compiled model uses [UnsafeAccessor] to reach non-public members and needs a concrete FieldInfo/MethodInfo; if none is found it throws InvalidOperationException with the error string produced by TryGetMemberInfo (CoreStrings.NoBackingField).

Source

Thrown at src/EFCore.Design/Scaffolding/Internal/CSharpRuntimeModelCodeGenerator.cs:1805

            property.AddRuntimeAnnotation(CoreAnnotationNames.UnsafeAccessors, accessors.Take(i).ToArray());
        }

        return memberAccessReplacements;
    }

    private QualifiedName? RegisterPrivateAccessor(
        IPropertyBase property,
        bool forMaterialization,
        bool forSet,
        string @namespace,
        BidirectionalDictionary<Type, string> unsafeAccessorClassNames,
        Dictionary<Type, HashSet<MemberInfo>> unsafeAccessorTypes,
        ref Dictionary<MemberInfo, QualifiedName>? memberAccessReplacements)
    {
        if (!property.TryGetMemberInfo(forMaterialization, forSet, out var member, out var error))
        {
            throw new InvalidOperationException(error);
        }

        if (member == null)
        {
            return null;
        }

        switch (member)
        {
            case FieldInfo field:
            {
                if (field.IsPublic
                    || (memberAccessReplacements?.ContainsKey(field)) == true)
                {
                    return null;
                }

                break;

View on GitHub (pinned to 3a2006ef56)

Solutions

  1. Explicitly configure the backing field with .HasField("_fieldName") or .UsePropertyAccessMode(PropertyAccessMode.Property).
  2. Name the backing field so EF discovers it by convention (e.g. _name, m_name, <Property>k__BackingField).
  3. Switch the access mode to one that does not require a field (e.g. PropertyAccessMode.Property).

Example fix

// before
modelBuilder.Entity<E>().Property(p => p.Secret).UsePropertyAccessMode(PropertyAccessMode.Field); // no field
// after
modelBuilder.Entity<E>().Property(p => p.Secret).HasField("_secret").UsePropertyAccessMode(PropertyAccessMode.Field);
Defensive patterns

Strategy: validation

Validate before calling

// Verify each property can resolve a backing member under its access mode
foreach (var prop in model.GetEntityTypes().SelectMany(e => e.GetProperties()))
{
    if (!prop.TryGetMemberInfo(forMaterialization: false, forSet: true, out _, out var error))
        Console.WriteLine($"{prop.DeclaringType}.{prop.Name}: {error}");
}

Try / catch

try { Optimize(); }
catch (InvalidOperationException ex) when (ex.Message.Contains("backing field"))
{ /* call .HasField("...") or change PropertyAccessMode, then regenerate */ }

Prevention

When it happens

Trigger: Compiled-model generation for a property with a PropertyAccessMode (e.g. Field, FieldDuringConstruction) that requires a backing field, but no backing field was discovered or configured.

Common situations: Using SetPropertyAccessMode/SetFieldAccessMode to Field when the property has no conventionally-discovered backing field and none was configured; auto-properties whose compiler-generated field name does not match the convention after renaming; fields marked in ways that defeat discovery.

Related errors


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