I have a column in a database table with a datatype of decimal(7, 3)
. When I enter this number (0.729
) in input box, it will be updated to 0.730
. I want to prevent this rounding. I have tried to set this data annotation to the field in the view model class, but it didn't work. I use ExecuteSqlInterpolatedAsync
to execute the stored procedure but I can't find any way to solve that issue.
The action in the controller:
[HttpPost]
public async Task<IActionResult> UpdateElements(updateProductElementsViewModel model)
{
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
if (!string.IsNullOrEmpty(userId))
{
await _appDbContext.Database.ExecuteSqlInterpolatedAsync($"exec SPApp_ProductElementsUpdate {userId},{model.id},{model.productName},{model.Density},{model.Ron}");
}
return RedirectToAction(nameof(HomeController.Index), "Home");
}
The stored procedure parameter:
@Density decimal(7,3)
The auto property on the updateProductElementsViewModel
class:
[Column(TypeName = "decimal(7,3)")]
public decimal Density { get; set; }
The OnModelCreating
method:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<updateProductElementsViewModel>()
.Property(x => x.Density)
.HasColumnType("decimal(7,3)");
base.OnModelCreating(modelBuilder);
}