aspnetboilerplate/aspnetboilerplate · error · ArgumentException

' ' cannot have a negative field length.

Error message

'{0}' cannot have a negative field length.

What it means

MemberMap.Size(int) validates that the database field length passed to the fluent mapping API is non-negative. A negative size cannot represent a valid column length, so the mapper throws ArgumentException naming the mapped property before assigning DbSize.

Solutions

  1. Inspect the call site passing the negative value and fix the arithmetic/literal so size >= 0
  2. Clamp or validate the value before calling Size: if (len < 0) len = 0;
  3. If the length is intentionally unbounded, remove the Size() call entirely instead of passing a sentinel negative value
  4. Wrap mapping construction in try/catch during startup to surface which member received the bad value

Example fix

// before
map.Map(x => x.Name).Size(-1);
// after
int size = Math.Max(0, configuredLength);
map.Map(x => x.Name).Size(size);
Defensive patterns

Strategy: validation

Validate before calling

if (size < 0) throw new ArgumentOutOfRangeException(nameof(size), size, "Field length must be non-negative");
map.Map(x => x.Name).Size(size);

Try / catch

try { map.Map(x => x.Name).Size(rawSize); }
catch (ArgumentException ex) when (ex.Message.Contains("negative field length"))
{
    logger.LogError(ex, "Invalid field length {Size} for {Member}", rawSize, nameof(x.Name));
}

Prevention

When it happens

Trigger: Calling Size() with a negative literal (e.g. .Size(-1)), or with a computed value derived from config/input that evaluates to < 0 (e.g. maxLen - overhead when overhead > maxLen).

Common situations: Building Dapper class mappings programmatically where the column length comes from a config file, database metadata, or subtraction arithmetic; off-by-one or sign errors when deriving lengths dynamically.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of aspnetboilerplate/aspnetboilerplate@2323c13a15 (2026-09-08). Data as JSON: /api/errors/71489a7e6ae7c1c4. Report an issue: GitHub.

Appendix: source

Thrown at src/Abp.Dapper/Dapper-Extensions/Mapper/MemberMap.cs:212

        {
            if (KeyType != KeyType.NotAKey && KeyType != KeyType.SlapperIdentifierKey)
            {
                throw new ArgumentException(string.Format("'{0}' is a key field and cannot be marked readonly.", Name));
            }

            IsReadOnly = true;
            return this;
        }

        /// <summary>
        /// Fluently sets the field length of the property
        /// </summary>
        /// <param name="size">The length of the field as it exists in the database</param>
        public MemberMap Size(int size)
        {
            if (size < 0)
            {
                throw new ArgumentException(string.Format("'{0}' cannot have a negative field length.", Name));
            }

            DbSize = size;
            return this;
        }

        /// <summary>
        /// Fluently sets the DbType of the property.
        /// </summary>
        public MemberMap Type(DbType dbType)
        {
            DbType = dbType;
            return this;
        }

        /// <summary>
        /// Fluently sets the ParameterDirection of the property
        /// </summary>

View on GitHub (pinned to 2323c13a15)