litedb-org/LiteDB · error · ArgumentNullException

field

Error message

field

What it means

Thrown by EntityBuilder.Field(string) when the field name argument is null, empty, or whitespace. The field name becomes the stored document key; a blank name cannot be mapped. Reached from the fluent mapping configuration API (BsonMapper.Entity<T>().Field(...)).

Source

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

        /// <summary>
        /// Define which property will not be mapped to document
        /// </summary>
        public EntityBuilder<T> Ignore<K>(Expression<Func<T, K>> member)
        {
            return this.GetMember(member, (p) =>
            {
                _entity.WaitForInitialization();
                _entity.Members.Remove(p);
            });
        }

        /// <summary>
        /// Define a custom name for a property when mapping to document
        /// </summary>
        public EntityBuilder<T> Field<K>(Expression<Func<T, K>> member, string field)
        {
            if (field.IsNullOrWhiteSpace()) throw new ArgumentNullException(nameof(field));

            return this.GetMember(member, (p) =>
            {
                p.FieldName = field;
            });
        }

        /// <summary>
        /// Define which property is your document id (primary key). Define if this property supports auto-id
        /// </summary>
        public EntityBuilder<T> Id<K>(Expression<Func<T, K>> member, bool autoId = true)
        {
            return this.GetMember(member, (p) =>
            {
                _entity.WaitForInitialization();
                
                // if contains another _id, remove-it
                var oldId = _entity.Members.FirstOrDefault(x => x.FieldName == "_id");

View on GitHub (pinned to f906a5f850)

Solutions

  1. Pass a concrete non-empty field name string.
  2. Validate the field name with string.IsNullOrWhiteSpace before configuring the mapping.
  3. If the goal is to keep the default name, omit the Field() call entirely.

Example fix

// before
builder.Field(x => x.UserName, fieldName); // fieldName may be empty

// after
if (string.IsNullOrWhiteSpace(fieldName))
    throw new InvalidOperationException("Field name is required.");
builder.Field(x => x.UserName, fieldName);
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrWhiteSpace(field))
    throw new ArgumentException("Field name is required.", nameof(field));

Type guard

static bool IsValidFieldName(string s) => !string.IsNullOrWhiteSpace(s);

Prevention

When it happens

Trigger: Calling builder.Field(x => x.Name, null), builder.Field(x => x.Name, ""), or builder.Field(x => x.Name, " "). Also when the field name is read from a config/attribute that is unset.

Common situations: Renaming a field and leaving the new name empty during refactor, building field names from property names with a transformation that yields empty, or passing a constant that was never filled in.

Related errors


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