AutoMapper
1.通过NuGet添加引用
2.新增AutoMapperConfig类处理映射配置
/// <summary> /// 映射配置 /// </summary> public class AutoMapperConfig { public static void RegisterMappings() { Mapper.Initialize(cfg => { cfg.CreateMap<City, CityDto>(); cfg.CreateMap<CityDto, City>(); }); } }
3.在Global.asax.cs的Application_Start方法中调用 AutoMapperConfig.RegisterMappings();
4.调用示例
City city = new City(); CityDto dto = AutoMapper.Mapper.Map<CityDto>(city); //将City实体映射到CityDto中