litedb-org/LiteDB · error · NotSupportedException

Member {name} not found on BsonMapper for type {member.Decla

Error message

Member {name} not found on BsonMapper for type {member.DeclaringType}.

What it means

Thrown by the private ResolveMember helper in LinqExpressionVisitor when a member accessed in a LINQ query is not found in the entity's mapped Members list. Unlike error 60 (which fires during EntityBuilder configuration), this fires during query translation when the visitor tries to resolve a member path against the BsonMapper's EntityMapper for the declaring type. The member name must match a MemberName in entity.Members.

Source

Thrown at LiteDB/Client/Mapper/Linq/LinqExpressionVisitor.cs:643

        /// <summary>
        /// Returns document field name for some type member
        /// </summary>
        private string ResolveMember(MemberInfo member, out MemberMapper memberMapper)
        {
            var name = member.Name;

            // checks if parent field are not DbRef (checks for same dataType)
            var isParentDbRef = _dbRefType != null && member.DeclaringType.IsAssignableFrom(_dbRefType);

            // get class entity from mapper
            var entity = _mapper.GetEntityMapper(member.DeclaringType);
            entity.WaitForInitialization();

            // get mapped field from entity
            var field = entity.Members.FirstOrDefault(x => x.MemberName == name);

            memberMapper = field ?? throw new NotSupportedException($"Member {name} not found on BsonMapper for type {member.DeclaringType}.");

            // define if this field are DbRef (child will need check parent)
            _dbRefType = field.IsDbRef ? field.UnderlyingType : null;

            // if parent call is DbRef and are calling _id field, rename to $id
            return "." + (isParentDbRef && field.FieldName == "_id" ? "$id" : field.FieldName);
        }

        /// <summary>
        /// Define if this method is index access and must eval index value (do not use parameter)
        /// </summary>
        private bool IsMethodIndexEval(MethodCallExpression node, out Expression obj, out Expression idx)
        {
            var method = node.Method;
            var type = method.DeclaringType;
            var pars = method.GetParameters();

            // for List/Dictionary [int/string]

View on GitHub (pinned to f906a5f850)

Solutions

  1. Ensure the member is a public instance property with a getter and is not marked [BsonIgnore].
  2. If the member is a field, set mapper.IncludeFields = true before querying.
  3. Register the member explicitly via mapper.Entity<T>().Field(x => x.Member, "field_name").
  4. Verify the member name in the lambda exactly matches a mapped MemberName.

Example fix

// before -- querying an unmapped field
col.Find(x => x._internalScore > 50);
// throws: Member '_internalScore' not found on BsonMapper

// after -- enable fields or map explicitly
var mapper = new BsonMapper { IncludeFields = true };
// or:
mapper.Entity<MyEntity>().Field(x => x._internalScore, "score");
col.Find(x => x._internalScore > 50);
Defensive patterns

Strategy: validation

Validate before calling

// Verify a member is mapped before using it in a query
var entityMapper = mapper.GetEntityMapper(typeof(T));
if (!entityMapper.Members.Any(m => m.MemberName == nameof(T.TargetMember)))
{
    throw new InvalidOperationException($"Member is not mapped.");
}
col.Find(x => x.TargetMember > 0);

Type guard

// Check if a member is mapped before querying
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
{
    var results = col.Find(x => x.UnmappedField > 0);
}
catch (NotSupportedException ex) when (ex.Message.Contains("not found on BsonMapper for type"))
{
    mapper.IncludeFields = true;
    results = col.Find(x => x.UnmappedField > 0);
}

Prevention

When it happens

Trigger: Querying x => x.UnmappedMember where the member is not in the entity's mapped Members: it could be a field (IncludeFields is false), have [BsonIgnore], lack a getter, be an indexer, or belong to a type whose mapping hasn't been configured. Also fires if a member of a nested type in the path is not mapped.

Common situations: Querying a public field without IncludeFields enabled. Querying a [BsonIgnore]'d member. Querying a member added to the class after the mapper was first used without reconfiguration. Querying a computed read-only property that has no backing field (though getters-only properties are mapped if CanRead is true -- the issue is usually fields or ignored members).

Related errors


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