Home » C# » Automapper » How to create a mapping to a primitive type with AutoMapper.

How to create a mapping to a primitive type with AutoMapper.

If you want to map one of your complex object to a primitive you cannot use the ForMember method of Automapper to do it. Instead, you have to use the ConvertUsing.

Here is a case of ComplexType which reprensent a boolean value.

	Mapper
	.CreateMap<ComplexType, bool>()
	.ConvertUsing(f=>f.ID);

	Mapper
	.CreateMap<bool, ComplexType>()
	.ConvertUsing(f => new ComplexType(f));

This is usefull if you have view models that represent primitive data and you want them to be represented into a view model object.

If you like my article, think to buy my annual book, professionally edited by a proofreader. directly from me or on Amazon. I also wrote a TypeScript book called Holistic TypeScript

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.