{"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/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Abstractions/PrecisionAttribute.cs#L8-L44","documentation":"The PrecisionAttribute(int precision, int scale) constructor validates that the precision argument is non-negative. Precision represents the maximum number of digits for a decimal property. A negative precision is meaningless for database column definitions and is rejected at attribute construction time.","triggerScenarios":"Applying [Precision(-1, 2)] to a decimal property — the constructor checks precision < 0 before assigning the field and throws ArgumentException with paramName \"precision\".","commonSituations":"Passing a computed or user-supplied precision value that can be negative; copy-paste errors where a sentinel like -1 is accidentally used; dynamic attribute construction from configuration data that may contain invalid values.","solutions":["Ensure the precision value passed to [Precision(precision, scale)] is zero or positive.","If the value comes from configuration, validate it is >= 0 before applying the attribute.","For decimal properties, typical precision values range from 1 to 38 depending on the database provider."],"exampleFix":"// before\n[Precision(-1, 2)]\npublic decimal Price { get; set; }\n\n// after\n[Precision(10, 2)]\npublic decimal Price { get; set; }","handlingStrategy":"validation","validationCode":"// Validate precision before constructing the attribute\nint precision = GetValueFromConfig();\nint scale = 2;\nif (precision < 0)\n{\n    throw new ArgumentOutOfRangeException(nameof(precision), \"Precision must be non-negative.\");\n}\nvar attr = new PrecisionAttribute(precision, scale);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["When applying [Precision] from configuration data, validate precision >= 0 before use.","For decimal properties, use realistic precision values (1-38).","Avoid using sentinel values like -1 for precision."],"tags":["ef-core","precision-attribute","model-validation","configuration"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}