class Person
{
Address Addr { get; set; }
int Age { get; set; }
}
class Address
{
string StreetName { get; set; }
County Cnty { get; set; }
}
class County
{
string CntyName;
string CntyCode;
}
這裏是我的存儲過程,它填充數據庫中的數據。嵌套多映射的小巧語法
Create Procedure SpGetAllPersons
As
Select CntyName, CntyCode, StreetName, Age from Persons
我試着寫下面短小精悍的查詢,但得到一個異常
DBConn.Query<County, Address , Person, Person>
(DomainConstants.SpGetAllPersons,
(cnty, address, person) =>
{
address.Cnty = cnty;
person.Addr = address;
return person;
},
commandType: CommandType.StoredProcedure,
splitOn: "StreetName, Age").ToList();
我試着用下面的概念,但它只返回單個對象。我需要一個人的名單。
var sql =
@"select
1 as PersonId, 'bob' as Name,
2 as AddressId, 'abc street' as Name, 1 as PersonId,
3 as Id, 'fred' as Name
";
var personWithAddress = connection.Query<Person, Address, Extra, Tuple<Person, Address, Extra>>
(sql, (p, a, e) => Tuple.Create(p, a, e), splitOn: "AddressId,Id").First();
在此先感謝。
老實說,我沒有看到這個塊的重點。它應該自動映射,這就是精巧的美麗;) – coffekid