4

I'm trying to perform a very standard multi mapping query using Dapper, and I'm getting the following error. I also get another error occasionally when this seems to work, but I'm unable to reproduce it at the moment. I'll append it to this post if/when the first problem is solved.

Here is the query code:

        const string storedProc = "dbo.GetStopsForRouteID";

        var stops = conn.Query<RouteStop, MapLocation, RouteStop>(
           storedProc, (stop, loc) =>
        {
            stop.Location = loc;
            return stop;
        }, new { RouteID = routeId }, commandType: CommandType.StoredProcedure);

In Dapper.cs on line 498:

var deserializer2 = (Func<IDataReader, TSecond>)info.OtherDeserializers[0];

info.OtherDeserializers is null which causes a NullReferenceException.

This is the guts of the stored procedure:

SELECT 
    RouteStops.StopID, 
    RouteStops.Name, 
    RouteStops.Description, 
    RouteStops.IsInbound, 
    RouteStops.Location.Lat as Latitude, 
    RouteStops.Location.Long as Longitude
FROM dbo.Routes

INNER JOIN dbo.StopsOnRoute ON
Routes.RouteID = StopsOnRoute.RouteID

INNER JOIN dbo.RouteStops ON
StopsOnRoute.StopID = RouteStops.StopID

WHERE Routes.RouteID = @RouteID
ORDER BY StopsOnRoute.SequenceNumber

I've had an extensive look at the dapper code but I can't find anything that seems out of place other than that TFirst's deserialiser isn't null, but TSecond's is. Could there be a problem when it creates TSecond's deserializer that leaves it as null?

Here are the types:

public class MapLocation
{
    public double Latitude { get; set; }
    public double Longitude { get; set; }
}

public class RouteStop {
    public int StopID { get; set; }

    public string Name { get; set; }
    public string Description { get; set; }
    public bool IsInbound { get; set; }

    public MapLocation Location { get; set; }
}
George
  • 1,964
  • 2
  • 19
  • 25

1 Answers1

2

Probably the main problem here is that you haven't told it how to "split"; try adding the parameter:

splitOn: "Latitude"

without that, as far as dapper can see there is no second result portion (it splits on Id by default).

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • Thanks, that fixed it. I was curious what splitOn was for but couldn't find any docs or a straight explanation of it. – George Oct 05 '11 at 05:43
  • @George that's fair critique; actually, on `Query<,,>` it states `The Field we should split and read the second object from (default: id)`, but that comment is not duplicated elsewhere – Marc Gravell Oct 05 '11 at 05:46
  • Spent a good 3 hours on that one, I'm sure many would appreciate it if the google code page was updated :) – George Oct 05 '11 at 06:04
  • 1
    @George I updated the documentation, hopefully this avoids this issue in future. – Sam Saffron Oct 05 '11 at 10:26