Multiple Dapper mapping - collections are empty
I am trying to use Dapper to return a set of Stocks and a one-to-many related 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 smoothed data that I expect. However, when I run the code through Dapper, the Items and History collections both return empty. I was screwing around with the splitOn parameter but after reading this question I now understand what splitOn does (it would be nice to have it somewhere on the Dapper btw site) and I Think I am getting through this part. So what am I doing wrong?
source to share
I don't think you can complete a 1-line deep object diagram. (If all elements are not in the same row) There's a similar question there: Populating a list in an object with dapper
Edit: There's also QueryMultiple
- you can check it out. It allows you to return multiple result sets. Then you can match your objects.
source to share