I am checking v1.25 Dapper with Sqlite through System.Data.Sqlite. If I run this query:
var rowCount = dbc.Query<int>("SELECT COUNT(*) AS RowCount FROM Data").Single();
I get the following error: System.InvalidCastException: Specified cast is not valid
This is because Sqlite returns the above value as Int64, which I can check with the following code. This will call "Int64":
var row = dbc.Query("SELECT COUNT(*) AS RowCount FROM Data").Single(); Type t = row.RowCount.GetType(); throw new System.Exception(t.FullName);
Now the following code will handle the conversion down from Int64 to Int32:
public class QuerySummary { public int RecordCount { get; set; } } var qs = dbc.Query<QuerySummary>("SELECT COUNT(*) AS RecordCount FROM Data").Single(); rowCount = qs.RecordCount; throw new System.Exception(rowCount.ToString());
When I throw this exception, it gives me the actual number of lines, indicating that Dapper handled the conversion for me.
My question is why dbc.Query<int> does not handle down conversion similarly to dbc.Query<QuerySummary> ? Is this the intended behavior?
John source share