Dapper multi-mapping - collections are empty

c# dapper

Question

I'm attempting to use Dapper to return a set of Shares and an associated one-to-many collection of ShareItems and ShareHistories. My Dapper call looks like this:

string sql =
    @"select s.Id, s.UserId, s.Name, si.ShareId as Id, si.Name as ItemName
    , sh.ShareId As Id, sh.DateShared, sh.SentTo 
    from Shares s 
    inner join ShareItems si on s.Id = si.ShareId
    inner join ShareHistory sh on s.Id = sh.ShareId
    where s.Id = @shareId";

    return conn.Query<Share, List<ShareItem>, List<ShareHistory>, Share>(
                sql,
                (share, shareItems, history) => 
                    { 
                      share.Items = shareItems; 
                      share.History = history; return share; 
                    },
                new { shareId = shareId }).Single();

When I run the query in SQL I get the flattened data I expect. However, when I run the code through Dapper the Items and History collections are coming back empty. I was screwing around with the splitOn parameter but after reading this question I now understand what splitOn is doing (this would be good to have somewhere on the Dapper site btw) and I think I'm handling that part okay. So what am I doing wrong?

Accepted Answer

I don't think you can populate a deep object graph from 1 row. (Unless all items are in that one row) There's a similar question: Populating a list in a object with dapper

Edit: There's also QueryMultiple - you might want to check that out. It allows the return of multiple resultsets. You could then map your entities.

Query Multiple Example




Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why