litedb-org/LiteDB · error · ArgumentNullException

Member '{member.GetPath()}' not found in type '{_entity.ForT

Error message

Member '{member.GetPath()}' not found in type '{_entity.ForType.Name}' (use IncludeFields in BsonMapper)

What it means

Thrown by the private GetMember helper in EntityBuilder when a member lambda (used by Id, Field, DbRef, or Ignore) does not match any MemberName in the entity's mapped Members list. GetMember resolves the lambda path via ExpressionExtensions.GetPath and looks it up against the Members that BuildEntityMapper populated from GetTypeMembers. The hint '(use IncludeFields in BsonMapper)' points at the most frequent cause: the target member is a field, and by default LiteDB only maps properties.

Source

Thrown at LiteDB/Client/Mapper/EntityBuilder.cs:108

            return this.GetMember(member, (p) =>
            {
                BsonMapper.RegisterDbRef(_mapper, p, _typeNameBinder, collection ?? _mapper.ResolveCollectionName(typeof(K)));
            });
        }

        /// <summary>
        /// Get a property based on a expression. Eg.: 'x => x.UserId' return string "UserId"
        /// </summary>
        private EntityBuilder<T> GetMember<TK, K>(Expression<Func<TK, K>> member, Action<MemberMapper> action)
        {
            if (member == null) throw new ArgumentNullException(nameof(member));
            _entity.WaitForInitialization();
            
            var memb = _entity.GetMember(member);

            if (memb == null)
            {
                throw new ArgumentNullException($"Member '{member.GetPath()}' not found in type '{_entity.ForType.Name}' (use IncludeFields in BsonMapper)");
            }

            action(memb);

            return this;
        }
    }
}

View on GitHub (pinned to f906a5f850)

Solutions

  1. If the member is a field, set mapper.IncludeFields = true before calling Entity<T>().
  2. Ensure the member is a public instance property with a getter and no index parameters.
  3. Remove [BsonIgnore] from the member if you need to map or configure it.
  4. Verify the lambda references a member directly on T (e.g., x => x.Name), not a nested member (e.g., x => x.Profile.Name).

Example fix

// before
var db = new LiteDatabase("my.db");
db.Mapper.Entity<MyEntity>().Field(x => x.MyField, "my_field");
// throws: Member 'MyField' not found in type 'MyEntity'

// after
db.Mapper.IncludeFields = true;
db.Mapper.Entity<MyEntity>().Field(x => x.MyField, "my_field");
Defensive patterns

Strategy: validation

Validate before calling

// Before calling EntityBuilder methods, verify the member is mapped
var entityMapper = mapper.GetEntityMapper(typeof(T));
if (!entityMapper.Members.Any(m => m.MemberName == "MyField"))
{
    // member is a field -- enable IncludeFields or it is ignored
    throw new InvalidOperationException($"Member 'MyField' is not mapped.");
}

Type guard

// Check if a member is mapped before configuring it
static bool IsMemberMapped<T>(BsonMapper mapper, string memberName)
{
    var em = mapper.GetEntityMapper(typeof(T));
    return em.Members.Any(m => m.MemberName == memberName);
}

Try / catch

try
{
    mapper.Entity<T>().Field(x => x.MyField, "my_field");
}
catch (ArgumentNullException ex) when (ex.Message.Contains("not found in type"))
{
    mapper.IncludeFields = true;
    mapper.Entity<T>().Field(x => x.MyField, "my_field");
}

Prevention

When it happens

Trigger: Calling mapper.Entity<T>().Id(x => x.MyField), .Field(x => x.MyField, "name"), .DbRef(x => x.MyField), or .Ignore(x => x.MyField) where MyField is a public field (not a property) and mapper.IncludeFields is false. Also fires when the member has [BsonIgnore], is write-only (no getter), is an indexer (has index parameters), is static, or the lambda targets a member of a nested type rather than T itself.

Common situations: Using a struct or DTO with public fields and assuming they are auto-mapped. Adding [BsonIgnore] to a member and later trying to configure it via EntityBuilder. Attempting EntityBuilder configuration before setting IncludeFields on a custom BsonMapper instance. Refactoring a property into a field and forgetting to enable IncludeFields.

Related errors


AI-assisted analysis of litedb-org/LiteDB@f906a5f850 (2026-08-13). Data as JSON: /api/errors/99b66b2bae519728. Report an issue: GitHub.