使用自动映射器映射多级嵌套对象

罗曼·季莫霍夫(Roman Timokhov)

我有dto对象:

公共类SwivelInfoToViewDTO {public ResponseValue值{get; 组; }

    public List<ResponseDocument> Documents { get; set; }

    public string Status { get; set; }
}

public class ResponseValue
{
    public string SerialNumber { get; set; }

    public string ProductCode { get; set; }

    public string ProductName { get; set; }
}

public class ResponseDocument
{
    public string Language { get; set; }

    public int DocumentTypeId { get; set; }

    public string DocumentType { get; set; }

}

我上课是sorce:

            public class SwivelInformationResponse
            {
                public ResponseValue Value { get; set; }

                public string Status { get; set; }
            }

            public class ResponseValue
            {
                [JsonProperty("serial_number")]
                public string SerialNumber { get; set; }

                [JsonProperty("product_code")]
                public string ProductCode { get; set; }

                public List<ResponseDocument> Documents { get; set; }
            }

            public class ResponseDocument
            {
                [JsonProperty("language")]
                public string Language { get; set; }

                [JsonProperty("document_type_id")]
                public int DocumentTypeId { get; set; }

                [JsonProperty("document_type")]
                public string DocumentType { get; set; }

            }

我使用自动映射器,我的个人资料如下所示:

        public MappingProfile()
        {
            CreateMap<SwivelInformationResponse, SwivelInfoToViewDTO>()
                .ForMember(x => x.Value, s => s.MapFrom(src => src.Value))
                .ForMember(x => x.Documents, s => s.MapFrom(src => src.Value.Documents));
        }

但是以某种方式我得到一个错误:错误映射类型。目的地会员:值

如何正确进行绑定?

维克托·特鲁索夫(Victor Trusov)

您忘记映射您的ResponseValue和ResponseDocument类。它不是同一类,因此您也需要映射它们。

public MappingProfile()
{
    CreateMap<SourceNamespace.ResponseValue, DtoNamespace.ResponseValue>();
    CreateMap<SourceNamespace.ResponseDocument, DtoNamespace.ResponseDocument>();
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章