2

Using Automapper 3 (upgrading is not an option), I am wondering how can I map an entity (src) to destination where the property in destination does NOT exist in source?

Let's call the property in the destination some non mapped "temp" or "calculation" property. Of course when mapping, AM fails because the property in destination was not found in source.

CreateMap<SystemConfiguration, SystemConfigurationModel>()
                .ForMember(dest => dest.UserRulesModel, opt => opt.MapFrom(src => src.UserRules));

In the "UserRulesModel", I have this temp property. I want AM to ignore it when mapping from the entity (DB) into the View Model (UserRulesModel)

UPDATE: UserRulesModel is a collection, as is UserRules.

thank you.

Ahmed ilyas
  • 5,722
  • 8
  • 44
  • 72

1 Answers1

1

You can configure that when you create the map from UserRules to UserRulesModel:

CreateMap<UserRules, UserRulesModel>()
    .ForMember(dest => dest.Temp, opt => opt.Ignore());

UPDATE

Let's say UserRules is a collection of UserRuleItem objects and UserRulesModel is a collection of UserRuleModelItem objects.

If there is a property in UserRuleModelItem that is not present in UserRuleItem, you can configure AutoMapper to ignore that property using the syntax I posted originally:

CreateMap<UserRuleItem, UserRuleModelItem>()
    .ForMember(dest => dest.Temp, opt => opt.Ignore());

The type of dest will be the type of object you are mapping to, which is UserRuleModelItem in this case.

OJ Raqueño
  • 4,471
  • 2
  • 17
  • 30
  • 1
    I dont quite understand how that will work since I do want to map the object all but 1 property. Are you saying to add this after the previous line? – Ahmed ilyas Apr 13 '18 at 21:04
  • 2
    OK this does not quite work. The dest does not have the "Temp" property. It's the right hand side that has the property (UserRulesModel). Also the UserRulesModel is a collection. – Ahmed ilyas Apr 13 '18 at 21:32