{"record":{"id":"f7573a44a59a9cde","repo":"dotnet/efcore","slug":"the-number-argument-cannot-be-a-negative-number","errorCode":null,"errorMessage":"The number argument cannot be a negative number.","messagePattern":"The number argument cannot be a negative number\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Abstractions/PrecisionAttribute.cs","lineNumber":26,"sourceCode":"///     For example, if the property is a <see cref=\"decimal\" />\n///     then this is the maximum number of digits.\n/// </summary>\n/// <remarks>\n///     See <see href=\"https://aka.ms/efcore-docs-modeling\">Modeling entity types and relationships</see> for more information and examples.\n/// </remarks>\n[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]\npublic class PrecisionAttribute : Attribute\n{\n    /// <summary>\n    ///     Initializes a new instance of the <see cref=\"PrecisionAttribute\" /> class.\n    /// </summary>\n    /// <param name=\"precision\">The precision of the property.</param>\n    /// <param name=\"scale\">The scale of the property.</param>\n    public PrecisionAttribute(int precision, int scale)\n    {\n        if (precision < 0)\n        {\n            throw new ArgumentException(AbstractionsStrings.ArgumentIsNegativeNumber, nameof(precision));\n        }\n\n        if (scale < 0)\n        {\n            throw new ArgumentException(AbstractionsStrings.ArgumentIsNegativeNumber, nameof(scale));\n        }\n\n        Precision = precision;\n        Scale = scale;\n    }\n\n    /// <summary>\n    ///     Initializes a new instance of the <see cref=\"PrecisionAttribute\" /> class.\n    /// </summary>\n    /// <param name=\"precision\">The precision of the property.</param>\n    public PrecisionAttribute(int precision)\n    {\n        if (precision < 0)","sourceCodeStart":8,"sourceCodeEnd":44,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Abstractions/PrecisionAttribute.cs#L8-L44","documentation":"Thrown by the PrecisionAttribute(int precision, int scale) constructor when the precision argument is negative. Precision (max digits) is meaningless below zero, so the constructor rejects it immediately. The error surfaces at attribute construction time, typically during model building or first use of the attribute.","triggerScenarios":"Applying [Precision(precision, scale)] where precision is < 0, or constructing new PrecisionAttribute(-1, 2). Also triggered by code that computes precision from user/env input without clamping.","commonSituations":"Reading precision from a config file, environment variable, or database and passing a sentinel like -1 meaning 'unset'. Off-by-one or sign errors in computed precision values. Copying a precision literal from another column and editing it incorrectly.","solutions":["Pass a non-negative precision value (0 or greater); for decimal columns a typical range is 1-38.","If precision comes from external input, validate or clamp it to >= 0 before applying the attribute/constructing it.","Use a sentinel-to-default mapping (e.g., -1 -> unset) outside the attribute, and only construct PrecisionAttribute with a valid non-negative number."],"exampleFix":"// before\n[Precision(-1, 2)]\npublic decimal Price { get; set; }\n\n// after\n[Precision(18, 2)]\npublic decimal Price { get; set; }","handlingStrategy":"validation","validationCode":"if (precision < 0)\n{\n    throw new ArgumentOutOfRangeException(nameof(precision), \"Precision must be non-negative.\");\n}\nvar attr = new PrecisionAttribute(precision, scale);","typeGuard":"static bool IsValidPrecision(int precision) => precision >= 0;","tryCatchPattern":null,"preventionTips":["Clamp precision from config/env to >= 0 before constructing the attribute.","Reserve sentinel values like -1 for 'unset' at the config layer, never inside the attribute.","Validate numeric inputs at the trust boundary rather than relying on the attribute to reject them."],"tags":["ef-core","modeling","precision","configuration","argument-validation"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}