C
C#3d ago
Dachi

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
c#
public class Product
{
public string VenueId { get; set; }
public string Id { get; set; }
public decimal Price { get; set; }
public decimal? DiscountedPrice { get; set; }
public bool? Enabled { get; set; }
public DateTime? DisabledUntil { get; set; }
public bool? InStock { get; set; }
}
c#
public class Product
{
public string VenueId { get; set; }
public string Id { get; set; }
public decimal Price { get; set; }
public decimal? DiscountedPrice { get; set; }
public bool? Enabled { get; set; }
public DateTime? DisabledUntil { get; set; }
public bool? InStock { get; set; }
}
Does anybody know why it happens?
11 Replies
Angius
Angius3d ago
See if flushing the cache before the query helps?
Dapper.SqlMapper.PurgeQueryCache()
Dapper.SqlMapper.PurgeQueryCache()
Also, what's the type of this column in the database?
Dachi
DachiOP3d ago
unfortunally i don't know the type. Db is external
Angius
Angius3d ago
So you don't even know the exact shape of data you're working with? Damn
Dachi
DachiOP3d ago
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
Angius
Angius3d ago
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
Dachi
DachiOP3d ago
it is like brand new. First time ran it in production i've never nees that type of error
Angius
Angius3d ago
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
Dachi
DachiOP3d ago
Okay thanks anyways
Angius
Angius3d ago
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
Dachi
DachiOP3d ago
Sure I did it. by the way Db type is oracle if it helps
Insire
Insire3d ago
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

Did you find this page helpful?