首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AutoMapper .ReverseMap() .Ignore()不工作

AutoMapper .ReverseMap() .Ignore()不工作
EN

Stack Overflow用户
提问于 2018-03-23 14:19:48
回答 2查看 1.7K关注 0票数 1

版本6.1.1有问题。在下面的示例中,反向映射的结果仍然填充了Company对象。Per this post,它显示了我下面正在做的事情,除了它们忽略了一个属性,而我忽略了一个复杂的对象。

我遗漏了什么?

代码语言:javascript
复制
CreateMap<Item, ItemViewModel>(MemberList.Destination)
.ReverseMap()
    .ForMember(x => x.Company, x => x.Ignore())
    ;
EN

回答 2

Stack Overflow用户

发布于 2018-03-23 19:28:48

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

代码语言:javascript
复制
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; }  
    }
}

}

票数 0
EN

Stack Overflow用户

发布于 2020-05-01 23:17:40

在AutoMapper 6.1中,您可以使用ForPath而不是ForMember来忽略复杂对象。有关详细信息,请参阅How to ignore property with ReverseMap

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49443520

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档