I'm struggling to get multi mapping working in dapper.
Here's my code:
var contactDictionary = new Dictionary<Guid, Contact>();
IEnumerable<Contact> contacts = connection.Query<Contact, Activity, Contact>(
@"SELECT ContactId, Title, FirstName, Surname,
AddressType.Type AS AddressType, Address.AddressLine1, Address.AddressLine2, Address.AddressLine3, Address.Town, Address.County, Address.Country, Address.Postcode,
EmailAddressType.Type AS EmailAddressType, EmailAddress.Email AS EmailAddress,
PhoneNumberType.Type As PhoneNumberType, PhoneNumber.Number AS PhoneNumber,
Direction.Type AS Direction, Activity.Summary, Activity.DateCompleted
FROM Contact
LEFT JOIN Address ON Contact.ContactPK = Address.ContactPK
LEFT JOIN AddressType ON Address.AddressTypePK = AddressType.AddressTypePK
LEFT JOIN EmailAddress ON Contact.ContactPK = EmailAddress.ContactPK
LEFT JOIN EmailAddressType ON EmailAddress.EmailAddressTypePK = EmailAddressType.EmailAddressTypePK
LEFT JOIN PhoneNumber ON Contact.ContactPK = PhoneNumber.ContactPK
LEFT JOIN PhoneNumberType ON PhoneNumber.PhoneNumberTypePK = PhoneNumberType.PhoneNumberTypePK
LEFT JOIN Activity ON Contact.ContactPK = Activity.ContactPK
LEFT JOIN Direction ON Activity.DirectionPK = Direction.DirectionPK
WHERE Contact.ContactId = @ContactId
AND Address.IsPrimary = 1 AND EmailAddress.IsPrimary = 1 AND PhoneNumber.IsPrimary = 1",
param:new { ContactId = contactId },
map:(c, a) =>
{
Contact contactEntry;
if (!contactDictionary.TryGetValue(c.ContactId, out contactEntry))
{
contactEntry = c;
contactEntry.Activities = new List<Activity>();
contactDictionary.Add(contactEntry.ContactId, contactEntry);
}
contactEntry.Activities.Add(a);
return contactEntry;
},
splitOn: "ContactId")
.Distinct()
.ToList();
Here's the result of the SQL when run in SSMS:
Here's the error I'm getting when the code executes: When using the multi-mapping APIs ensure you set the splitOn param if you have keys other than Id Parameter name: splitOn
I'm really confused because I thought I had specified the splitOn parameter?
Any help would be appreciated.
The splitOn parameter should indicate to dapper how to separate properties from both entities (Contact and Activity), you should set it to activity's first property (assuming your properties are ordered in the SQL statement).