. Netcore using AutoMapper

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.