SqlException: Arithmetic overflow error converting numeric to data type numeric.
I ran into this SQL Server error when using Entity Framework in a .NET Core project. It's a simple mistake easy to make. The mistake I made was creating a decimal column with incorrect settings.
When setting up a decimal SQL column, you define its precision and scale. For Entity Framework you can use HasColumnType when adding the map. Here's how it would look in SQL.
HoursRecorded decimal(4,2)
The above states the decimal should have 4 digits, with 2 digits being to the right of the decimal. That means 99.99 is valid but 999.9 and 9.999 would run into issues.
A common error is to want the above 4 digit decimal with 2 digits on both sides of the decimal but write the SQL as HoursRecorded decimal(2,2)
. This results in 2 digit decimal with 0 digits on the left side.
If you attempt to use a number greater than 0.99 on a decimal(2,2)
you will receive the SqlException: Arithmetic overflow error converting numeric to data type numeric. error.
One Last Thing...
If you have a question or see a mistake, please comment below.
If you found this post helpful, please share it with others. It's the best thanks I can ask for & it gives me momentum to keep writing!