Using automapper__.net in. NET Core

Source: Internet
Author: User
1. Create a new class
Using AutoMapper; Using Yourmodels; Using Yourviewmodels; Namespace YourNameSpace {public class Automapperprofileconfiguration:profile {protected override void Configure () {Cr Eatemap<application, applicationviewmodel> (); Createmap<applicationviewmodel, application> (); ... } } 2, adding mapperconfiguration properties to Startup.cs
Private mapperconfiguration _mapperconfiguration {get; set;} 3, increasing in the startup method in Startup.cs
_mapperconfiguration = new Mapperconfiguration (cfg => {cfg). Addprofile (New Automapperprofileconfiguration ()); }); 4. Increase in configureservices ()
Services. Addsingleton<imapper> (SP => _mapperconfiguration.createmapper ()); 5. Use
Using AutoMapper; Using ... namespace YourNameSpace {public class Applicationscontroller:basecontroller {[fromservices] private Imapper _mapper {get; set;} [Fromservices] Private iapplicationrepository _applicationrepository {get; set;} Public Applicationscontroller ( Imapper Mapper, Iapplicationrepository applicationrepository) {_mapper = mapper; _applicationrepository = Applicationrepository; }//Get:applications public async task<iactionresult> Index () {ienumerable<application> applications = Awai T _applicationrepository.getforidasync (...); if (applications = null) return Httpnotfound (); List<applicationviewmodel> ViewModel = _mapper. Map<list<applicationviewmodel>> (applications);

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.