{"record":{"id":"71489a7e6ae7c1c4","repo":"aspnetboilerplate/aspnetboilerplate","slug":"0-cannot-have-a-negative-field-length","errorCode":null,"errorMessage":"'{0}' cannot have a negative field length.","messagePattern":"'(.+?)' cannot have a negative field length\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Abp.Dapper/Dapper-Extensions/Mapper/MemberMap.cs","lineNumber":212,"sourceCode":"        {\n            if (KeyType != KeyType.NotAKey && KeyType != KeyType.SlapperIdentifierKey)\n            {\n                throw new ArgumentException(string.Format(\"'{0}' is a key field and cannot be marked readonly.\", Name));\n            }\n\n            IsReadOnly = true;\n            return this;\n        }\n\n        /// <summary>\n        /// Fluently sets the field length of the property\n        /// </summary>\n        /// <param name=\"size\">The length of the field as it exists in the database</param>\n        public MemberMap Size(int size)\n        {\n            if (size < 0)\n            {\n                throw new ArgumentException(string.Format(\"'{0}' cannot have a negative field length.\", Name));\n            }\n\n            DbSize = size;\n            return this;\n        }\n\n        /// <summary>\n        /// Fluently sets the DbType of the property.\n        /// </summary>\n        public MemberMap Type(DbType dbType)\n        {\n            DbType = dbType;\n            return this;\n        }\n\n        /// <summary>\n        /// Fluently sets the ParameterDirection of the property\n        /// </summary>","sourceCodeStart":194,"sourceCodeEnd":230,"githubUrl":"https://github.com/aspnetboilerplate/aspnetboilerplate/blob/2323c13a152aa0ebfb37af3830f687d7f5b53795/src/Abp.Dapper/Dapper-Extensions/Mapper/MemberMap.cs#L194-L230","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Inspect the call site passing the negative value and fix the arithmetic/literal so size >= 0","Clamp or validate the value before calling Size: if (len < 0) len = 0;","If the length is intentionally unbounded, remove the Size() call entirely instead of passing a sentinel negative value","Wrap mapping construction in try/catch during startup to surface which member received the bad value"],"exampleFix":"// before\nmap.Map(x => x.Name).Size(-1);\n// after\nint size = Math.Max(0, configuredLength);\nmap.Map(x => x.Name).Size(size);","handlingStrategy":"validation","validationCode":"if (size < 0) throw new ArgumentOutOfRangeException(nameof(size), size, \"Field length must be non-negative\");\nmap.Map(x => x.Name).Size(size);","typeGuard":null,"tryCatchPattern":"try { map.Map(x => x.Name).Size(rawSize); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"negative field length\"))\n{\n    logger.LogError(ex, \"Invalid field length {Size} for {Member}\", rawSize, nameof(x.Name));\n}","preventionTips":["Clamp computed lengths with Math.Max(0, value) before calling Size","Never use negative sentinels for 'unbounded'; omit Size() instead","Unit-test mapping construction with edge-case config values"],"tags":["csharp","dapper","argument-out-of-range","orm-mapping"],"backgroundTag":"invalid-argument-value","analyzedSha":"2323c13a152aa0ebfb37af3830f687d7f5b53795","analyzedAt":"2026-09-08T08:00:50.638Z","contentChangedAt":"2026-09-08T08:00:50.638Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}