AutoMapper复杂类型对象映射C#

时间:2016-06-06 06:55:14

标签: c# automapper complextype

在我的方法中,我得到了包含几个字符串列表和复杂类型对象列表的结果。

            var AcctLst = gd.validateGroupMembershipUploadDetails(_input);


            Mapper.CreateMap<Data.Entities.Upload.GroupMembershipValidationOutput, Business.Upload.GroupMembershipValidationOutput>();

            var result = Mapper.Map<Data.Entities.Upload.GroupMembershipValidationOutput, Business.Upload.GroupMembershipValidationOutput>(AcctLst);


            return result;

var AcctLst包含样本数据:

enter image description here

Data.Entities.Upload.GroupMembershipValidationOutput

Business.Upload.GroupMembershipValidationOutput

看起来像

public class GroupMembershipValidationOutput
{
    public List<string> _validMasterIds { get; set; }
    public List<ChapterCodeValidationOutput> _validChapterCodes { get; set; }
    public List<GroupCodeValidationOutput> _validGroupCodes { get; set; }

    public List<string> _invalidMasterIds { get; set; }
    public List<string> _invalidChapterCodes { get; set; }
    public List<string> _invalidGroupCodes { get; set; }
}


public class ChapterCodeValidationOutput
{
    public string chpt_cd { get; set; }
    public string appl_src_cd { get; set; }
}



public class GroupCodeValidationOutput
{
    public string grp_cd { get; set; }
    public string grp_nm { get; set; }
}

我猜_validChapterCodes和_validGroupCodes抛出以下异常:

Missing type map configuration or unsupported mapping.

Mapping types:
ChapterCodeValidationOutput -> ChapterCodeValidationOutput
ARC.Donor.Data.Entities.Upload.ChapterCodeValidationOutput -> ARC.Donor.Business.Upload.ChapterCodeValidationOutput

Destination path:
GroupMembershipValidationOutput._validChapterCodes._validChapterCodes._validChapterCodes0[0]

Source value:
ARC.Donor.Data.Entities.Upload.ChapterCodeValidationOutput

1 个答案:

答案 0 :(得分:2)

是的,该死的。它相对简单。您只需要首先映射内部嵌套类型。

    var AcctLst = gd.validateGroupMembershipUploadDetails(_input);
    Mapper.CreateMap<Data.Entities.Upload.ChapterCodeValidationOutput, Business.Upload.ChapterCodeValidationOutput>();
    Mapper.CreateMap<Data.Entities.Upload.GroupCodeValidationOutput, Business.Upload.GroupCodeValidationOutput>();
    Mapper.CreateMap<Data.Entities.Upload.GroupMembershipValidationOutput, Business.Upload.GroupMembershipValidationOutput>();

    var result = Mapper.Map<Data.Entities.Upload.GroupMembershipValidationOutput, Business.Upload.GroupMembershipValidationOutput>(AcctLst);
    return result;

然后它完美无缺。