您的位置:首页 > 移动开发

how to use automapper in c#, from cf~

2016-04-01 14:00 531 查看
[DataContract]
public class GroupDto
{
[DataMember]
public int id { get; set; }
[DataMember]
public string name{ get; set; }
[DataMember]
public List<UserDTO> Users { get; set; }
}
2- Create your mappings :

Mapper.CreateMap<User, UserDto>();
Mapper.CreateMap<UserDto, User>(); // Only if you convert back from dto to entity

Mapper.CreateMap<Group, GroupDto>();
Mapper.CreateMap<GroupDto, Group>(); // Only if you convert back from dto to entity
3- that's all, because auto mapper will automatically map the List<User> to List<UserDto> (since they have same name, and there is already a mapping from user to UserDto)

4- When you want to map you call :

Mapper.Map<GroupDto>(groupEntity);
Hope that helps.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: