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.

Matt Ferderer

Software engineer who spends his time learning about building teams, project management, software architecture, C#, .NET Core, Blazor, JavaScript, TypeScript, Azure, user experience, web security, and performance.

comment

Comments