Mapping Exclusions with AutoMapper
AutoMapper is a popular library for creating mappings between object types. While it simplifies the mapping process, there may be instances when you need to exclude specific properties from the mapping.
Ignoring a Property in AutoMapper
In the scenario described, you have an OrderModel class with a ProductName property that is not present in the Orders database. To prevent the mapping of this property, you can use the Ignore() method in your mapping configuration:
CreateMap<OrderModel, Orders>().ForMember(x => x.ProductName, opt => opt.Ignore());
By adding the Ignore() method, you tell AutoMapper to skip the ProductName property during the mapping process.
Updates to AutoMapper
In previous versions of AutoMapper, the Ignore() method was used. However, in version 8.0 and later, the method has been replaced with DoNotValidate().
ForSourceMember:
https://github.com/AutoMapper/AutoMapper/blob/master/docs/8.0-Upgrade-Guide.md
This change ensures that the property will not be validated during the mapping process.
The above is the detailed content of How to Exclude Properties from Mapping with AutoMapper?. For more information, please follow other related articles on the PHP Chinese website!