dotnet / dotnet/SqlClient

Improvement - Not truncating/rounding decimals for `SqlParameter` with `Scale = 0`, and throws Out of Range Exception

Open
#1,214 8 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
989
Forks
340
Avg merge
4d 18h
Merged PRs (30d)
69

Description

After upgrading from 3.1 to 5 my decimal types started throwing an Out of Range Exception on save. I know there's a breaking change related, but even after setting the precision using the new API I'm still getting this error.

My previous configuration was working in 3.1:

```
builder.Property(e => e.BookValuePerSqmtr).HasColumnType("decimal(14,0)");
```
Trying to save this number 22351.969005269646026090843821 throws the out of Range exception, while it was doing the right rounding/truncating in 3.1.

```
builder.Property(e => e.BookValuePerSqmtr).HasPrecision(14,0);
```

I tried changing the model builder configuration to use the new HasPrecision method but I'm still getting the same error. Is there any way to re enable the rounding before saving?

```
Microsoft.EntityFrameworkCore.DbUpdateException: 'An error occurred while updating the entries. See the inner exception for details.'
ArgumentException: Parameter value '22351.969005269646026090843821' is out of range.
```

EF Core version: 5.0.7
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 5.0
Operating system: Windows 10
IDE: Visual Studio 2019 16.3

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.