AutoMapper .ReverseMap()。Ignore()不起作用

时间:2018-03-23 06:19:48

标签: automapper automapper-6

版本6.1.1存在问题。在下面,反向映射的结果仍然填充了Company对象。每this post,显示我在下面做的事情,除非他们忽略了一个属性,我忽略了一个复杂的对象。

我错过了什么?

CreateMap<Item, ItemViewModel>(MemberList.Destination)
.ReverseMap()
    .ForMember(x => x.Company, x => x.Ignore())
    ;

2 个答案:

答案 0 :(得分:0)

我看不出有什么问题,但这是一个正在运行的样本:

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; }  
    }
}

}

答案 1 :(得分:0)

在AutoMapper 6.1中,您可以使用ForPath代替ForMember来忽略复杂的对象。 有关更多信息,请参见How to ignore property with ReverseMap