C# 十进制的最佳数据注释(18,2)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19811180/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 15:56:08  来源:igfitidea点击:

Best Data annotation for a Decimal(18,2)

c#asp.net-mvcentity-framework

提问by John John

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 property, inside my asp.net MVC web application ?

我的 sql server 2008 中有一个列,类型为Decimal(18,2). 但是在实体框架上,我可以在我的 asp.net MVC Web 应用程序中应用于此属性的最佳数据注释验证是什么?

回答by ediblecode

There is no explicit data annotation for a decimal so you need to use two separate ones to add constraints.

小数没有明确的数据注释,因此您需要使用两个单独的注释来添加约束。

Two Decimal Points

两个小数点

[RegularExpression(@"^\d+\.\d{0,2}$")]

This regular expression will make sure that the property has at most two decimal places.

此正则表达式将确保该属性最多有两位小数。

Max 18 digits

最多 18 位

[Range(0, 9999999999999999.99)]

Assuming you aren't accepting any negative numbers. Otherwise, replace 0with -9999999999999999.99.

假设您不接受任何负数。否则,替换0-9999999999999999.99

Result

结果

[RegularExpression(@"^\d+\.\d{0,2}$")]
[Range(0, 9999999999999999.99)]
public decimal Property { get; set; }

回答by Schmalls

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

我认为@jumpingcode 的答案可以合二为一RegularExpressionAttribute

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

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

这可以用于任何precisionscale。16 被替换为precision- scale,而 2 被替换为scale。正则表达式匹配应该像输入的数字###0.##.##0,和###.##以及为负值。

回答by mgalpy

This seems to be the correct answer ( the above answers either restrict valid numbers that can be inserted into a data type of Decimal(18,2) or cause compile errors if you apply them to your code -- please confirm for yourself):

这似乎是正确的答案(上述答案要么限制了可以插入 Decimal(18,2) 数据类型的有效数字,要么在将它们应用于代码时导致编译错误——请自行确认):

Use the following two constraints together:

一起使用以下两个约束:

Two Decimal Points

两个小数点

[RegularExpression(@"^\d+.?\d{0,2}$", ErrorMessage = "Invalid Target Price; Maximum Two Decimal Points.")]

Max 18 digits

最多 18 位

  [Range(0, 9999999999999999.99, ErrorMessage = "Invalid Target Price; Max 18 digits")]

回答by mjyazdani

 [Range(1,(double) decimal.MaxValue, ErrorMessage="value should be between{1} and {2}."]

回答by Adam Diament

For a different approach which some may consider more readable, you can override the OnModelCreating method of your DbContext to set precision, like so:

对于某些人可能认为更具可读性的不同方法,您可以覆盖 DbContext 的 OnModelCreating 方法以设置精度,如下所示:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {

           modelBuilder.Entity<YourEntity>()
                    .Property(x => x.TheProprty)
                    .HasPrecision(18, 2);
    }

Advantage: strongly typed vs custom regular expression

优势:强类型 vs 自定义正则表达式

Disadvantage: can't see it on the class with just a scan

缺点:仅扫描在课堂上看不到

回答by ransems

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

我几乎完全使用(b/c 它很简单而且有效)

[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

回答by Breeno

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

继@Schmalls 示例(并评论将其重新构建到属性中)之后,我创建了一个工作示例(使用 C# 6 字符串插值):

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 99.99")]
public decimal TotalCost { get; set; }

回答by Daniel Jacobson

If you write the 'column' annotation, will work fine

如果您编写“列”注释,则可以正常工作

    [Required]
    [Column(TypeName = "decimal(18, 6)")]
    public decimal Foo { get; set; }