【问题标题】:Dapper multi-mapping errorDapper 多映射错误
【发布时间】:2017-12-22 18:45:49
【问题描述】:

我正在努力让多映射在 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 结果:

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

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

任何帮助将不胜感激。

【问题讨论】:

  • splitOn 参数应该指示 dapper 如何从两个实体(联系人和活动)中分离属性,您可能应该将其设置为活动的第一个属性(假设它们是有序的)。
  • 谢谢,将 splitOn 更改为“Direction”就成功了

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


【解决方案1】:

splitOn 参数应该向 dapper 指示如何从两个实体(联系人和活动)中分离属性,您应该将其设置为活动的第一个属性(假设您的属性在 SQL 语句中排序)。

【讨论】:

    猜你喜欢
    • 2019-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-27
    相关资源
    最近更新 更多