AutoMapper with CreateMissingTypeMaps and AddProfile

I use AutoMapper with profiles and it works very well. I recently found the CreateMissingTypeMaps configuration and, if it works, as I understand it, will speed up my development, once when you do not need to create simple mappings.

I added a profile with AddProfile to my configuration and then created a CreateMissingTypeMaps configuration.

Example:

------- Creating a configuration ------------

var config = new MapperConfiguration(cfg => { cfg.AddProfile<PersonServiceMapperProfile>(); cfg.CreateMissingTypeMaps = true; }); 

------- Inside my PersonServiceMapperProfile ------

 internal class PersonServiceMapperProfile : Profile { protected override void Configure() { CreateMap<PersonData, ProfileViewModel>().ConvertUsing<PersonDataToProfileViewModel>(); } } 

If the CreateMissingTypeMaps configuration is set to true, the profile no longer works. My user mapping is not called.

How to solve this issue?

+5
source share
1 answer

I ran into this problem after upgrading from AutoMapper 3 to 6 recently, and I found a problem on GitHub for AutoMapper that revealed a solution to this problem:

AddConditionalObjectMapper () is a way for your types of anon to appear, but not overwrite default mappings. Do not use this with CreateMissingtypeMaps because it overwrites the condition. CreateMissingTypeMaps is AddConditionalObjectMapper (). Where ((s, d) => true);

Source: 2008 edition, TylerCarlson1

However, this should not be used for "simple comparisons." The CreateMissingTypeMaps function is intended for use only when the mappings are unknown until compilation, for example using EF Dynamic Proxies. The reason it should not be used for known types is because the CreateMap and compilation functions allow you to quickly test and prevent bad mappings due to typos, incorrect names, new or deleted properties, etc. It is very important to use the compilation function for its intended purpose, so make the most of it.

0
source

Source: https://habr.com/ru/post/1246907/


All Articles