Best Data annotation for a Decimal(18,2)

后端 未结 8 1300
無奈伤痛
無奈伤痛 2020-12-14 05:57

I have a column inside my sql server 2008 wih type of Decimal(18,2). But on entity framework what is the best data annotation validation I can apply to this pro

相关标签:
8条回答
  • 2020-12-14 06:50

    I think @jumpingcode's answer can be combined into one RegularExpressionAttribute.

    [RegularExpression(@"^(0|-?\d{0,16}(\.\d{0,2})?)$")]
    public decimal Property
    {
        get;
        set;
    }
    

    This can be used for any precision and scale. The 16 is replaced by precision - scale and the 2 is replaced by the scale. The regular expression should match numbers entered like ###, 0.##, .##, 0, and ###.## as well as negative values.

    0 讨论(0)
  • 2020-12-14 06:52

    Im using almost excplusively (b/c it's simple and works)

    [Range(typeof(decimal), "0", "1")]
    public decimal Split { get; set; }
    

    Then if I need to convert back to double I add a conversion

    (double)model.Split
    
    0 讨论(0)
提交回复
热议问题