Home > Backend Development > C++ > How to Ignore Properties During AutoMapper Mappings?

How to Ignore Properties During AutoMapper Mappings?

Mary-Kate Olsen
Release: 2024-12-27 09:46:11
Original
567 people have browsed it

How to Ignore Properties During AutoMapper Mappings?

Ignoring Properties in Automapper Mappings

When working with Automapper, it can be necessary to exclude certain properties from mapping between source and destination objects. This can occur when the destination object contains read-only or calculated properties that would not be suitable for mapping from the source.

One such scenario is where the source object (e.g., OrderModel) has a property (ProductName) that is not present in the destination object (e.g., Orders). Attempting to perform the mapping with:

Mapper.CreateMap<OrderModel, Orders>();
Copy after login

Will result in an exception indicating that the ProductName property is unmapped.

To prevent Automapper from attempting to map the ProductName property, use the Ignore() method:

CreateMap<OrderModel, Orders>().ForMember(x => x.ProductName, opt => opt.Ignore());
Copy after login

Alternatively, in AutoMapper versions after 8.0, the Ignore() method has been replaced with DoNotValidate():

CreateMap<OrderModel, Orders>().ForMember(x => x.ProductName, opt => opt.DoNotValidate());
Copy after login

By specifying the Ignore() or DoNotValidate() option, Automapper will exclude the ProductName property from the mapping process, resolving the exception and allowing the mapping to proceed successfully.

The above is the detailed content of How to Ignore Properties During AutoMapper Mappings?. 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