Home > Backend Development > C++ > How to Exclude Properties from Mapping with AutoMapper?

How to Exclude Properties from Mapping with AutoMapper?

Barbara Streisand
Release: 2024-12-28 13:27:10
Original
514 people have browsed it

How to Exclude Properties from Mapping with AutoMapper?

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());
Copy after login

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
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template