使用dapper链接多个类
本文关键字:链接 dapper 使用 | 更新日期: 2023-09-27 18:33:51
例如我有两个类
class User
{
string name {get;set;}
int age {get;set;}
Register reg {get;set;}
}
class Register
{
datetime time {get; set;}
bool active {get;set;}
}
我已将查询设置为匹配属性,但我想将值映射到类中的值。
我怎样才能让它在 dapper 中工作?
您可以使用带有 spliton 参数的多映射查询。比较 http://www.tritac.com/bp-24-dapper-net-by-example:
public class Account {
public int? Id {get;set;}
public string Name {get;set;}
public string Address {get;set;}
public string Country {get;set;}
public int ShopId {get; set;}
public Shop Shop {get;set;}
}
public class Shop {
public int? ShopId {get;set;}
public string Name {get;set;}
public string Url {get;set;}
}
var resultList = conn.Query<Account, Shop, Account>(@"
SELECT a.Name, a.Address, a.Country, a.ShopId
s.ShopId, s.Name, s.Url
FROM Account a
INNER JOIN Shop s ON s.ShopId = a.ShopId
", (a, s) => {
a.Shop = s;
return a;
},
splitOn: "ShopId"
).AsQueryable();