开发者

Writing Dapper Query for Nested Objects

开发者 https://www.devze.com 2023-03-08 16:06 出处:网络
I have a code Structure as below: class Person { Name PersonName; int Age; } class Name { string FirstName { get; set; }

I have a code Structure as below:

class Person
{
    Name PersonName;
    int Age;
}

class Name
{
    string FirstName { get; set; }
    string LastName { get; set; }
}

Here is my Stored Proc which populates the data from Database.

Create Procedure SpGetAllPersons
As
Select FirstName, LastName, Age from Persons

How do I write Dapper query which pulls all the Person from Database?

Example:

List<Person> Persons = DbConn.Query<Person>("SpGetAllPersons", 开发者_如何学编程CommandType.StoredProcedure);


You need to use the multi mapper if you want to select nested objects.

This should work:

List<Person> persons = DbConn.Query<Name,Person,Person>
  ("SpGetAllPersons",
     (name,person) => {person.Name = name; return person;} 
     commandType: CommandType.StoredProcedure, 
     splitOn: "Age");

The multi-mapper can return any type, even just an aggregate type that is not mapped to any db table.

It is important to supply the splitOn param if you intend to split on anything that is not called id or Id.

0

精彩评论

暂无评论...
验证码 换一张
取 消