Specify Decimal Column Type to Avoid Silent Truncation of Long Values
The database will silently truncate decimal values that do not fit the column size unless you explicitly specify the size of the column
You can override the OnModelCreating
method of the DbContext
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<Product>()
.Property(d => d.Price)
.HasPrecision(18, 2);
}
Or you can simply annotate the property with the [Precision(18, 2)]
attribute and achieve the same result.