Automapper 9.x , Mapping only if source members are not null , not working why ?

57 views
Skip to first unread message

Kasumović Mario

unread,
Sep 21, 2019, 7:33:40 PM9/21/19
to AutoMapper-users
Hello ,

I have upgraded old Automapper to newest , now my code don't run like expected.
       
       
        class DemoDTO
        {
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public int? VacationDays { get; set; }
            public DateTime? BirthDay { get; set; }
            public Double? LoanAmount { get; set; }
        }

            var config = new MapperConfiguration(cfg => {
                cfg.AllowNullCollections = true;
                cfg.CreateMap<DemoDTO, DemoDTO>()
                .ForAllMembers(opt => opt.Condition((src, dest, srcMember, destMember) => srcMember != null));
            });
            IMapper iMapper = config.CreateMapper();
            #endregion

            var destination = new DemoDTO()
            {
                FirstName = "M",
                LastName = "M",
                LoanAmount = 2000
            };
            var source = new DemoDTO() { FirstName = "Micky", LastName = "Mouse" };



            destination = iMapper.Map<DemoDTO, DemoDTO>(source);

I am updating my entity from WebAPI , if source have any NULL values this should not be mapped with my  DESTINATION

Executing this mapping , overwrite all my fields with source , and Ignorre my Condition , bug or why happend this ?


Reply all
Reply to author
Forward
0 new messages