dotnet/efcore · error · ArgumentException

The number argument cannot be a negative number.

Error message

The number argument cannot be a negative number.

What it means

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.

Solutions

  1. Ensure the precision value passed to [Precision(precision, scale)] is zero or positive.
  2. If the value comes from configuration, validate it is >= 0 before applying the attribute.
  3. For decimal properties, typical precision values range from 1 to 38 depending on the database provider.

Example fix

// before
[Precision(-1, 2)]
public decimal Price { get; set; }

// after
[Precision(10, 2)]
public decimal Price { get; set; }
Defensive patterns

Strategy: validation

Validate before calling

// Validate precision before constructing the attribute
int precision = GetValueFromConfig();
int scale = 2;
if (precision < 0)
{
    throw new ArgumentOutOfRangeException(nameof(precision), "Precision must be non-negative.");
}
var attr = new PrecisionAttribute(precision, scale);

Prevention

When it happens

Trigger: Applying [Precision(-1, 2)] to a decimal property — the constructor checks precision < 0 before assigning the field and throws ArgumentException with paramName "precision".

Common situations: 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.

Related errors


AI-assisted analysis of dotnet/efcore@3a2006ef56 (2026-08-11). Data as JSON: /api/errors/f7573a44a59a9cde. Report an issue: GitHub.

Appendix: source

Thrown at src/EFCore.Abstractions/PrecisionAttribute.cs:26

///     For example, if the property is a <see cref="decimal" />
///     then this is the maximum number of digits.
/// </summary>
/// <remarks>
///     See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see> for more information and examples.
/// </remarks>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
public class PrecisionAttribute : Attribute
{
    /// <summary>
    ///     Initializes a new instance of the <see cref="PrecisionAttribute" /> class.
    /// </summary>
    /// <param name="precision">The precision of the property.</param>
    /// <param name="scale">The scale of the property.</param>
    public PrecisionAttribute(int precision, int scale)
    {
        if (precision < 0)
        {
            throw new ArgumentException(AbstractionsStrings.ArgumentIsNegativeNumber, nameof(precision));
        }

        if (scale < 0)
        {
            throw new ArgumentException(AbstractionsStrings.ArgumentIsNegativeNumber, nameof(scale));
        }

        Precision = precision;
        Scale = scale;
    }

    /// <summary>
    ///     Initializes a new instance of the <see cref="PrecisionAttribute" /> class.
    /// </summary>
    /// <param name="precision">The precision of the property.</param>
    public PrecisionAttribute(int precision)
    {
        if (precision < 0)

View on GitHub (pinned to 3a2006ef56)