我有一个代码结构如下:
class Person
{
Name PersonName;
int Age;
}
class Name
{
string FirstName { get; set; }
string LastName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这是我的存储过程,它填充数据库中的数据.
Create Procedure SpGetAllPersons
As
Select FirstName, LastName, Age from Persons
Run Code Online (Sandbox Code Playgroud)
如何编写从数据库中提取所有Person的Dapper查询?
例:
List<Person> Persons = DbConn.Query<Person>("SpGetAllPersons", CommandType.StoredProcedure);
如果要选择嵌套对象,则需要使用多映射器.
这应该工作:
List<Person> persons = DbConn.Query<Name,Person,Person>
("SpGetAllPersons",
(name,person) => {person.Name = name; return person;}
commandType: CommandType.StoredProcedure,
splitOn: "Age");
Run Code Online (Sandbox Code Playgroud)
多映射器可以返回任何类型,甚至只返回未映射到任何db表的聚合类型.
它是提供重要的splitOn,如果你打算拆就任何不叫PARAM id或Id.
| 归档时间: |
|
| 查看次数: |
5256 次 |
| 最近记录: |