开发者

How do I get AutoMapper to map this?

开发者 https://www.devze.com 2022-12-23 02:03 出处:网络
Say I have this class: public class Account { public int 开发者_StackOverflow社区AccountID { get; set; }

Say I have this class:

public class Account
{
    public int 开发者_StackOverflow社区AccountID { get; set; }
    public Enterprise Enterprise { get; set; }
    public List<User> UserList { get; set; }
}

When I use AutoMapper to map the Account class, I would also like it to map the Enterprise class, and the list of users (UserList) in the returned object. How can I get AutoMapper to do this?

Thanks!


AutoMapper does that out of-the-box if you provide a configuration for the Enterprise and User type.

Configuration looks like this:

Mapper.CreateMap<Account, AccountDto>();
Mapper.CreateMap<Enterprise, EnterpriseDto>();
Mapper.CreateMap<User, UserDto>();

This shows how to how collections get mapped: http://automapper.codeplex.com/wikipage?title=Lists%20and%20Arrays&referringTitle=Home


You need to create a mapping for each pair of types you would like mapped.

Mapper.CreateMap<Account, AccountDto>();
Mapper.CreateMap<Enterprise, EnterpriseDto>();
Mapper.CreateMap<User, UserDto>();

Order is not important.

0

精彩评论

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

关注公众号