Models vs. View Models
Occasionally you will require the ability to separate a model for a view, from the equivalent model for a database. But what happens when you need to transfer the view model data to the database model object?
AutoMapper is an object-object mapper, and makes this mapping task fairly straight-forward (albeit, following some configuration).
- Install a copy of “AutoMapper.Extensions.Microsoft.DependencyInjection” from Nuget.
- Create a view model.
- Create a model.
- Create an AutoMapper “Profile” (class which inherits from the Profile class in AutoMapper), for defining the map between the view model and the model.
- Configure Services in Startup.cs to add AutoMapper to the project.
- Create a service for executing the model mapping.
- Configure Services in Startup.cs for your mapping service (as a transient service).