Software Environment
- vs2015
- ASP. NET MVC 5
- . NET Framework 4.5.2
- AutoMapper 5.2.0.0
AutoMapper Installation
New ASP. NET MVC Project Automapperexample, where you should use VS to create a new MVC project, no more
,
Click Tools →NUGETB Package Manager → Manage the NuGet package for the solution, in the popup screen, select "Browse", enter AutoMapper, go to search, select the first one in the search out package, then install
The output, as shown, indicates that the installation AutoMapper successfully
AutoMapper Configuration
With static global mapper registration, it should be placed at the time of application startup, the Application_Start () method of the Global.asax file for ASP.
Create a new AutoMapper folder in your program that holds the class for object mappings under which the new class Automapperconfig, which handles all object mappings
This class deals primarily with the mapping of objects, that is, from one object to another
Public classAutomapperconfig { Public Static voidConfig () {mapper.initialize (cfg={cfg. Createmap<orderdto, order>(); Cfg. Addprofile<ExtendMapProfile>(); }); } Public classExtendmapprofile:profile {protected Override voidConfigure () {Createmap<userdto, user>(); } } }
Where Order,orderdto,user,userdto is the entity object, where the,createmap<> is no longer described in the automapper conversion of two of the objects, where the following will be described in detail, and then add a new object conversion, Createmag<source,dest> conversions are performed here
The static method is executed in the Application_Start () method of the Global.asax file
protected void Application_Start () { automapperconfig.config (); Arearegistration.registerallareas (); Filterconfig.registerglobalfilters (globalfilters.filters); Routeconfig.registerroutes (routetable.routes); Bundleconfig.registerbundles (bundletable.bundles); }
At this point, all automapper configuration is complete
Introduction and use of AutoMapper (i)