forked from schotime/NPoco
-
Notifications
You must be signed in to change notification settings - Fork 0
Mapping to Nested Objects
schotime edited this page Nov 10, 2012
·
1 revision
These helpers allow you to map a query to an object that has nested objects. Given the following classes:
public class User
{
public int UserId { get; set; }
public string Name { get; set; }
public Address Address { get; set; }
}
public class Address
{
public string Street { get; set; }
public string City { get; set; }
}and the following query:
IDatabase db = new Database("connStringName");
var users = db.Fetch<User, Address>("select u.UserId, u.Name, u.Street, u.City from Users");This will give you a list of User objects with the nested class Address mapped.
Note:
- The order of the columns is extremely important. The columns in the query need to be specified in the same order as the generic parameters are specified. eg. The
Usercolumns are specified first, then theAddresscolumns next. - If you are mapping to objects that are also used for inserting data then you will need to make sure you ignore the
Addressproperty using the[Result]attribute.