1

版本 6.1.1 有问题。在下面,反向映射的结果仍然填充了 Company 对象。根据这篇文章,它显示了我在下面所做的事情,除了他们忽略了一个属性,而我忽略了一个复杂的对象。

我错过了什么?

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

2 回答 2

1

使用 AutoMapper 6.1,您可以改用ForPath忽略ForMember复杂对象。有关详细信息,请参阅如何使用 ReverseMap 忽略属性

于 2020-05-01T15:17:40.313 回答
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; }  
    }
}

}

于 2018-03-23T11:28:47.967 回答