Convert Dapper and Down Integer

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?

+6
source share
1 answer

No, this is not intentional. I made and pushed the changes to github that make the next pass (it does not work on 1.25); he should also appear on NuGet soon:

  // http://stackoverflow.com/q/23696254/23354 public void DownwardIntegerConversion() { const string sql = "select cast(42 as bigint) as Value"; int i = connection.Query<HasInt32>(sql).Single().Value; Assert.IsEqualTo(42, i); i = connection.Query<int>(sql).Single(); Assert.IsEqualTo(42, i); } 
+4
source

Source: https://habr.com/ru/post/969374/


All Articles