Invalid Operation Exception when trying to get decimal value from SQL into double

.net-core .net-core-2.2 c# entity-framework-core sql-server

Question

I have a SQL table which has several large decimal values in them. Too large to use decimals in my model. However when I use double I get an Invalid Operation Exception which states that it was unable to convert System.Decimal to System.Double.

I'm using EntityFramework Core in a .Net Core 2.2 application. Is there a way to specify to my DbContext that it should grab these fields as doubles instead of decimals?

I am using a Microsoft SQL Server database. My column type is decimal(38, 2). The value that's stored there is ~ 1.0005e+35. I know that this value is too big to fit into a C# Decimal so I need to fit it into a System.Double instead.

1
1
2/11/2019 10:30:02 PM

Accepted Answer

I figured it out. Apparently EF Core designates the different SQL types directly to C# types. So a SQL decimal maps to a C# Decimal. A SQL float maps to a C# System.Double. And a SQL real maps to a C# System.Float.

So the solution to my issue was to change the decimal(38,2) to a float in my SQL table

2
2/11/2019 7:35:07 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow