了解Dappers splitOn属性

时间:2015-05-10 00:51:04

标签: c# tsql dapper

我刚刚开始使用Dapper进行我正在进行的项目。我已经启动并运行并进行了一些简单的查询,但是当涉及到连接表并使用splitOn属性时,我有点困惑。

说我有以下查询:

Query<Firm, Firm_FirmType, FirmTypeShareholder, City, Country, Firm>(@"SELECT DISTINCT Firm.*, sh.CustomerID FROM Firm
                                    JOIN Firm_FirmType AS ft ON ft.FirmID = Firm.ID
                                    JOIN FirmTypeShareholder AS sh ON sh.Firm_FirmTypeID = ft.ID
                                    JOIN City ON City.ID = Firm.ZipCode
                                    JOIN Country ON Country.ID = Firm.ISO
                                    JOIN Person_Firm_PersonResponsibility as res ON res.FirmID = Firm.ID
                                    WHERE res.PersonID = " + personId,
                            (firm, firmType, shareholder, city, country) =>
                            {
                                firm.City = city;
                                firm.Country = country;
                                firmType.FirmTypeShareholder = shareholder;
                                firm.Firm_FirmType.Add(firmType);
                                return firm;
                            }, splitOn: "ID, ID, Firm_FirmTypeID, ID, ID");

因为我只想实际选择Firm表中的所有列,而只选择CustomerID表中的FirmTypeShareholder,我的splitOn到底应该怎样?现在,我已经为Query<...>中的每个表定义了要拆分的ID,这样就可以了:

  

使用多映射API时,如果您拥有Id以外的密钥,请确保设置splitOn参数

异常。

任何人都可以了解这是如何运作的(以及我做错了什么)? :-)提前致谢!

1 个答案:

答案 0 :(得分:4)

经过几个小时的头痛后发现自己:-)只需命名ID,我就可以了:

return conn.Query<Firm, Firm_FirmType, FirmTypeShareholder, City, Country, Firm>(@"SELECT DISTINCT Firm.*, ft.*, sh.CustomerID, ci.ID AS cityId, ci.ZipCode, ci.City, co.ID AS countryId, co.Country  FROM Firm
                                    JOIN Firm_FirmType AS ft ON ft.FirmID = Firm.ID
                                    JOIN FirmTypeShareholder AS sh ON sh.Firm_FirmTypeID = ft.ID
                                    JOIN City AS ci ON ci.ID = Firm.ZipCode
                                    JOIN Country AS co ON co.ID = Firm.ISO
                                    JOIN Person_Firm_PersonResponsibility as res ON res.FirmID = Firm.ID
                                    WHERE res.PersonID = " + personId,
                            (firm, firmType, shareholder, city, country) =>
                            {
                                firm.City = city;
                                firm.Country = country;
                                firmType.FirmTypeShareholder = shareholder;
                                firm.Firm_FirmType.Add(firmType);
                                return firm;
                            }, splitOn: "ID,ID,CustomerID,cityId,countryId");