Dapper多映射错误

时间:2017-12-22 18:45:49

标签: c# asp.net linq asp.net-core dapper

我正努力让多映射在精致的工作中发挥作用。

这是我的代码:

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();    

这是在SSMS中运行时SQL的结果: enter image description here

这是我在代码执行时遇到的错误: 使用多映射API时,如果您拥有Id以外的密钥,请确保设置splitOn参数 参数名称:splitOn

我真的很困惑,因为我认为我已经指定了 splitOn 参数?

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

splitOn参数应指示如何将属性与两个实体(Contact和Activity)分开,应将其设置为activity的第一个属性(假设您的属性在SQL语句中排序)。