how to use automapper in c#, from cf~

时间:2022-01-08 03:34:08
[DataContract]
public class GroupDto
{
[DataMember]
public int id { get; set; }
[DataMember]
public string name{ get; set; }
[DataMember]
public List<UserDTO> Users { get; set; }
}
- 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
- 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) - When you want to map you call : Mapper.Map<GroupDto>(groupEntity);
Hope that helps.