从基础获取数据后,需要将其从模型UserModel引入模型PatientModel。我这样做:
var _patients = patients.Select(p => new MyPatientModel
{
Id = p.Id,
PatientEmail = p.Email,
PatientFirstName = p.FirstName,
PatientSecondName = p.SecondName,
PatientThirdName = p.ThirdName,
});
return _patients;
但是我需要使用AutoMapper来完成相同的任务。我在启动中初始化了这些模型,所以:
config.CreateMap<MyPatientModel, User>();
请帮助解决此问题
答案 0 :(得分:1)
只需使用您的类型:
var config = new MapperConfiguration(
cfg => cfg.CreateMap<Patient, MyPatientModel>()
.ForMember(myP => myP.Id, opt => opt.MapFrom(p => p.Id))
.ForMember(myP => myP.PatientEmail, opt => opt.MapFrom(p => p.Email))
.ForMember(myP => myP.PatientFirstName, opt => opt.MapFrom(p => p.FirstName))
.ForMember(myP => myP.PatientSecondName, opt => opt.MapFrom(p => p.SecondName))
.ForMember(myP => myP.PatientThirdName, opt => opt.MapFrom(p => p.ThirdName))
);
var mapper = config.CreateMapper();
List<MyPatientModel> myPatientList = mapper.Map<List<Patient>, List<MyPatientModel>>(patients);