Add Reference
AutoMapper
AutoMapper.Extensions.Microsoft.DependencyInjection
Registration Services
Services. Addautomapper ();
Configure Mappings
Services. Addautomapper (); The services that are needed in AutoMapper, including the configuration class profile for AutoMapper
So how to use this profile, you only need to customize a class to inherit the profiles, in the constructor to configure the mapping relationship
Public class Custommapping:profile { public custommapping () { createmap<sys_userlogininfo, loginmsg>() ; Createmap<loginmsg, sys_userlogininfo>(); } }
Here has di custommapping, so instantiation, the constructor has also been executed naturally, do not need to go to Di Custommapping class, In the AutoMapper.Extensions.Microsoft.DependencyInjection
Addautomapper has done these things, including the Imapper interface
The Imapper object can be injected into the automapper where it is needed by the constructor function.
such as constructor injection
imapper _mapper; Public Usercontroller (imapper mapper) { = mapper; }
_mapper. Map<loginmsg, sys_userlogininfo> (loginmsg);
Additional instructions: When using the automapper process, if the fields in the class are inconsistent, you need to implement the mapping.
Createmap<sys_userlogininfo, Loginmsg> (). Formember (C=>c.username,x=>x.mapfrom (k=>k.use_use));
Map Mapfrom field from where to formember which field
Attention
Createmap<source,destination> (); Target from source
. Netcore using AutoMapper