【问题标题】:Mapping multi-level nestingobjects using automapper使用自动映射器映射多级嵌套对象
【发布时间】:2020-01-16 17:41:06
【问题描述】:

我有 dto 对象:

公共类 SwivelInfoToViewDTO { 公共响应值值 { 获取;放; }

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

}

我有我的魔法课:

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

但不知何故我得到一个错误: 错误映射类型。 目的地会员: 价值

如何正确进行绑定?

【问题讨论】:

    标签: c# .net-core automapper


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-01
      • 1970-01-01
      • 2021-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-27
      相关资源
      最近更新 更多