1

Having an issue with version 6.1.1. In the below, the result of the reverse map still has the Company object populated. Per this post, which shows what I am doing below, except they are ignoring a property, and I'm ignoring a complex object.

What am I missing?

CreateMap<Item, ItemViewModel>(MemberList.Destination)
.ReverseMap()
    .ForMember(x => x.Company, x => x.Ignore())
    ;
crichavin
  • 4,672
  • 10
  • 50
  • 95

2 Answers2

1

With AutoMapper 6.1 you could use ForPath instead ForMember to ignore complex objects. See How to ignore property with ReverseMap for further information.

user7217806
  • 2,014
  • 2
  • 10
  • 12
0

I see not what is wrong, but here is a running sample:

namespace AutomapperTest2
{
    internal class Program
    {
        #region Methods

        private static void Main(string[] args)
        {
            // Configure the mappings
            Mapper.Initialize(cfg =>
            {
                cfg.CreateMap<ApplicantEducation, ApplicantEducationVM>();
                cfg.CreateMap<Applicant, ApplicantVM>().ReverseMap()
                    .ForMember(x => x.Education, x => x.Ignore());
            });


            var config = new MapperConfiguration(cfg => cfg.CreateMissingTypeMaps = true);
            var mapper = config.CreateMapper();

            Applicant ap = new Applicant
            {
                Name = "its me",
                Education =
                    new ApplicantEducation
                    {
                        SomeInt = 10,
                        SomeString = "sampleString"
                    }
            };
            // Map
            ApplicantVM apVm = Mapper.Map<Applicant, ApplicantVM>(ap);
            Applicant apBack = Mapper.Map<ApplicantVM, Applicant>(apVm);
        }

        #endregion
    }


    /// Your source classes
    public class Applicant
    {   
        public ApplicantEducation Education { get; set; }
        public string Name { get; set; }   
    }

    public class ApplicantEducation
    {
        public int SomeInt { get; set; }
        public string SomeString { get; set; }    
    }

    // Your VM classes
    public class ApplicantVM
    { 
        public string Description { get; set; }
        public ApplicantEducationVM Education { get; set; }
        public string Name { get; set; }            
    }


    public class ApplicantEducationVM
    {    
        public int SomeInt { get; set; }
        public string SomeString { get; set; }  
    }
}

}

Stephu
  • 3,236
  • 4
  • 21
  • 33