【发布时间】:2017-09-25 19:34:43
【问题描述】:
在我的 asp.net 核心模型中,当我搭建数据库时,它只允许一个带有两个小数位的数字,例如 0.10,但是我不允许在我的数据库中插入四个小数位。
下面是我的模型。
public class Order
{
public int OrderID { get; set; }
[RegularExpression(@"^\d+.\d{0,4}$", ErrorMessage = "Must have four decimal places")]
[Range(0.0001, 1)]
[Display(Name = "Goal Diameter Tolerance")]
public decimal? GoalDiameterTolerance { get; set; }
}
下面是脚手架的 GoalDiameterTolerance。它只允许两位小数。我可以更改它,但是如何在模型中的脚手架之前修复它。
GoalDiameterTolerance = table.Column<decimal>(type: "decimal(18, 2)", nullable: true),
我相信它应该为此提供支架。
GoalDiameterTolerance = table.Column<decimal>(type: "decimal(18, 4)", nullable: true),
这是我解决问题的方法。
foreach (var property in modelBuilder.Model
.GetEntityTypes()
.SelectMany(t => t.GetProperties())
.Where(p => p.ClrType == typeof(decimal) ||
p.ClrType == typeof(decimal?))
.Select(p => modelBuilder.Entity(p.DeclaringEntityType.ClrType).Property(p.Name))
)
{
property.HasColumnType("decimal(18,4)");
}
【问题讨论】:
-
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:#.####}")] 是另一种可能行不通的方法。
标签: c# asp.net-core entity-framework-core data-annotations asp.net-core-2.0