简体   繁体   English

要在对象,精简程序中列出的多重映射字符串

[英]Multimapping string to list in object, dapper

I'm all new to Dapper and I'm trying to get my head around how it actually maps things. 我是Dapper的新手,我正在努力弄清它实际上是如何映射事物的。 I have the following database structure: 我具有以下数据库结构:

calendar
| Id | Name |

meeting_room
| Id | Calendar_id | Capacity |

meeting_room_purpose
| id | Name | Description |

meeting_room_meeting_room_purpose
| id | room_id | purpose_id |

The 'calendar' table is much like an account. “日历”表很像一个帐户。 It can have multiple meeting rooms associated with it. 它可以有多个与之关联的会议室。 A meeting room can have multiple purposes, which is the predicament of my question. 会议室可以有多种用途,这是我的问题的困境。 I need to have an object like this: 我需要一个这样的对象:

class MeetingRoom {
     public string Name { get; set; }
     public int Capacity { get; set; }
     public List<string> Purpose { get; set; }
}

So from calendar, I get the name, meeting_room the capacity and the list of purposes from all tuples with a purpose for the given room. 因此,从日历中,我从所有元组(具有给定房间的用途)获得名称,Meeting_room的容纳人数和用途列表。 My query is like this: 我的查询是这样的:

SELECT  
    calendar.id as CalendarId,
    calendar.name as name,
    meeting_room.capacity as capacity,
    purpose
FROM calendar
LEFT JOIN meeting_room ON meeting_room.calendar_id=calendar.id
INNER JOIN 
    (SELECT 
         meeting_room_purpose.name as purpose
     FROM
         meeting_room_purpose
     INNER JOIN meeting_room_meeting_room_purpose mrmrp ON
                meeting_room_purpose.id=mrmrp.purpose_id 
                AND mrmrp.room_id=@Id)
         a
     WHERE meeting_room.id=@Id    

The output of this query is a set of tuples, which differ only in purpose, eg 该查询的输出是一组元组,它们的目的只是不同,例如

| CalendarId |      name      | capacity |        purpose        |
      53         Charmander         6         Internal Meetings
      53         Charmander         6         Marketing Meetings 
      53         Charmander         6         Sales Meetings   

So, the meeting room "Charmander", has a capacity of 6 people, and can be used for 3 purposes. 因此,“ Charmander”会议室可容纳6人,可用于3个目的。 It should map to the object as (had it had a simple constructor): 它应该以以下方式映射到该对象(如果它具有简单的构造函数):

new MeetingRoom("Charmander", 6, new List<string>(){"Internal Meetings", "Marketing Meetings", "Sales Meetings"); 

My dapper query is as: 我的小巧的查询是:

_connection.Query<MeetingRoom, string, MeetingRoom>(sql, 
    (room, purpose) => { 
        room.Purpose = purpose; return room; 
    }, 
    new { Id = query.Id }, splitOn: "id, purpose").SingleOrDefault();

If I remove splitOn i get an exception, and if I keep it I just get an empty (null) object. 如果我删除splitOn,我会得到一个异常,如果保留它,我只会得到一个空(空)对象。 I'm sure it's not too hard, I just can't seem to get my "eurika" moment. 我确定这并不难,我似乎无法抓住我的“ eurika”时刻。

Best Regards 最好的祝福

Dapper's multimapping feature isn't really designed for this One to Many style mapping. Dapper的多重映射功能并不是真正为这种“一对多”样式映射设计的。 This SO Answer shows a way to load the rooms and purposes separately then join them in code afterward. 该SO Answer显示了一种单独加载房间和目的,然后将它们加入代码中的方法。 The QueryMultiple example does a similar thing with one database roundtrip. QueryMultiple示例对一个数据库往返执行类似的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM