Best Data annotation for a Decimal(18,2)

后端 未结 8 1312
無奈伤痛
無奈伤痛 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:49

    Following on from @Schmalls example (and comment re building it into an attribute) I've created a working example (uses C# 6 string interpolation):

    public class PrecisionAndScaleAttribute : RegularExpressionAttribute
    {
        public PrecisionAndScaleAttribute(int precision, int scale) : base($@"^(0|-?\d{{0,{precision - scale}}}(\.\d{{0,{scale}}})?)$")
        {
    
        }
    }
    

    Usage:

    [PrecisionAndScale(6, 2, ErrorMessage = "Total Cost must not exceed $9999.99")]
    public decimal TotalCost { get; set; }
    

提交回复
热议问题