每行类型切换时的多映射

时间:2018-06-05 06:34:51

标签: c# dapper

我想知道如何在Dapper中组合多映射每行类型切换

我有以下两个类,从同一个表中检索数据(称为 Person ):

public class Individual {
    public int Id  { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public Country DomicileCountry { get; set; }
}

public class Company {
    public int Id  { get; set; }
    public string Name{ get; set; }
    public Country DomicileCountry { get; set; }
}

国家/地区类如下:

public class Country {
    public int Id { get; set; }
    public string Iso3166 { get; set; }
}

我的select语句如下所示:

SELECT
    per.Id        AS Id,
    per.FirstName AS FirstName,
    per.LastName  AS LastName,
    per.LastName  AS Name,      // needed for Company-Class
    cnt.Id        AS Id,
    cnt.Iso3166   AS Iso3166
FROM
    Person per

LEFT OUTER JOIN
    Country cnt
ON
    cnt.Id = per.DomicileCountryId

Person 表中只有一种类型(例如Individual),所有对象都非常简单:

dbConnection.Query<Individual, Country, Individual>(sql, (ind, cnt) => {
    ind.DomicileCountry = cnt;
    return ind;
});

我知道Dapper的type switching feature但是我很难找到一种方法将它与多映射结合起来,因为RowParser似乎只能解析为单个对象。

0 个答案:

没有答案