如何使用 Dapper 从 SQL 查询加载数据到模型?
How to load data from SQL query to model using Dapper?
型号
Public class EmployeeGroup
{
Public int Total {get ;set}
Public IEnumerable<Employee>{get ;set}
}
Public class Employee
{
Public string Name{get ;set}
Public Location Location {get ;set}
Public Branch Branch {get ;set}
}
Public class Location
{
Public string Id {get ;set}
Public string Description {get ;set}
}
Public class Branch
{
Public string Id {get ;set}
Public string Description {get ;set}
}
SQL查询
Select name,l.id,locname,b.id,branchName from
emp e
left join location l on e.locId=l.Id
left join branch b on e.branchId=b.Id
请告诉我如何将 属性 about 查询数据映射到 model.I 在提供记录计数和其他分页内容的查询之上还有一个查询,所以我需要根据结构实现此要求。
Dapper 不支持您在此处尝试使用的投影类型 - 我建议您创建一个 看起来像数据 的 DTO 类型(即具有匹配的属性列),并使用 Query<T>
该类型 ,然后 单独 将其扩展到您的模型。
型号
Public class EmployeeGroup
{
Public int Total {get ;set}
Public IEnumerable<Employee>{get ;set}
}
Public class Employee
{
Public string Name{get ;set}
Public Location Location {get ;set}
Public Branch Branch {get ;set}
}
Public class Location
{
Public string Id {get ;set}
Public string Description {get ;set}
}
Public class Branch
{
Public string Id {get ;set}
Public string Description {get ;set}
}
SQL查询
Select name,l.id,locname,b.id,branchName from
emp e
left join location l on e.locId=l.Id
left join branch b on e.branchId=b.Id
请告诉我如何将 属性 about 查询数据映射到 model.I 在提供记录计数和其他分页内容的查询之上还有一个查询,所以我需要根据结构实现此要求。
Dapper 不支持您在此处尝试使用的投影类型 - 我建议您创建一个 看起来像数据 的 DTO 类型(即具有匹配的属性列),并使用 Query<T>
该类型 ,然后 单独 将其扩展到您的模型。