Dapper to c# decimal conversion fail
When executing script where I retrieve properties, one of them being Price, it throws error:
System.InvalidCastException: Unable to cast object of type 'System.Decimal' to type 'System.Double'.
delivery-connector-worker | at Deserializec435cf3c-5d1c-45f9-9ce4-096c88b38636(DbDataReader)
delivery-connector-worker | --- End of inner exception stack trace ---
delivery-connector-worker | at Dapper.SqlMapper.ThrowDataException(Exception ex, Int32 index, IDataReader reader, Object value) in /_/Dapper/SqlMapper.cs:line 3966
delivery-connector-worker | at Deserializec435cf3c-5d1c-45f9-9ce4-096c88b38636(DbDataReader)
delivery-connector-worker | at Dapper.SqlMapper.QueryAsync[T](IDbConnection cnn, Type effectiveType, CommandDefinition command)
delivery-connector-worker | at Trony.DeliveryConnector.Infrastructure.Data.DapperProductRepository.<>cDisplayClass7_0.<<GetActiveProductsAsync>b0>d.MoveNext().
The weird part is that Price in c# is of a type decimal. I don't have properties that are marked as double.
This is product class that it has to match
Does anybody know why it happens?
11 Replies
See if flushing the cache before the query helps?
Also, what's the type of this column in the database?
unfortunally i don't know the type. Db is external
So you don't even know the exact shape of data you're working with?
Damn
yes but I don't get the error message.
Unable to cast object of type 'System.Decimal' to type 'System.Double'
if casting failed it means that db returned type decimal but in c# code there was double?
or i misunderstood
Yeah, it sounds weird. I'd expect it to be the other way around
Hence cleaning the cache, it could be that some old version of the mapping is still in the cache, where the property was a double
it is like brand new. First time ran it in production
i've never nees that type of error
Huh
I'm not too well-versed in Dapper, unfortunately, I pretty much only ever use EF
So besides the cache, and double-checking the type in the db, I might not be of much help
Okay thanks anyways
If I don't come up with something, and nobody else chips in, you can link this thread in the #database channel too
It would help if you maybe posted the query you're trying to execute, and the relevant bit of C# code too
Knowing the types of the table columns would help too
Sure I did it. by the way Db type is oracle if it helps
run a select query on the product table without dapper and but raw ado.net instead. you can instruct ado.net to return only the table schema. do that and inspect the schema, then check whether your model actually matches the schema. i suspect the price column is not a decimal type or what ever oracle uses for as high precision floating point numbers