locked
Automapper ignore prefix per map instead of globally RRS feed

  • Question

  • User565442435 posted

    I want to remove a prefix from my source object, but I'd like to do it for each map an not globally, as it could cause issues where it's removing a column when it shouldn't. Is there an easy way to do this? This is what I'm working with right now

    public partial class MappingProfile : Profile
    {
    
    
        public MappingProfile()
        {
            //I would prefer this not to be a global and only remove the prefix on the specific map below
            RecognizePrefixes("Can");
            CreateMap<Candidate, CandidateVM>();
        }
    }

    Wednesday, April 18, 2018 7:30 PM

All replies

  • User-166373564 posted

    Based on your requirement, the profile is a good way to organize your mapping configuration, and the configuration within a profile would only be applied to maps inside a profile.

    Mapper.Initialize(cfg =>
    {
        cfg.AddProfile(new MyProfile1());
        cfg.CreateMap<A, C>();
    });
    
    public class MyProfile1 : Profile
    {
        public MyProfile1()
        {
            this.RecognizeDestinationPrefixes("Cust_");
            this.CreateMap<A, B>();
        }
    }

    I did not found any other better approaches, you could try to follow here for potential tutorials.

    Moreover, for the profile approach, you could automatically scan the profiles across assmeblies, details you could follow the section about Assembly Scanning for auto configuration.

    Thursday, April 19, 2018 6:24 AM