将一个对象映射到另一个复杂对象

时间:2018-12-18 18:17:13

标签: c# automapper

我具有以下“请求和响应”结构:

public class Action
{
    public class Create
    {
        public class Request
        {
            public string operationdate { get; set; }
            public string action { get; set; }
            public string identificationaccount { get; set; }
            public string observation { get; set; }
            private ErrorModel.Error errorrow = new ErrorModel.Error();
            public ErrorModel.Error ErrorRow { get { return errorrow; } set { errorrow = value; } }
        }
        public class Response : Request
        {
            public Int32 version { get; set; }
            public string groupcode { get; set; }
            public bool active { get; set; }
        }
    }
}

我想映射到以下DTO结构:

public class GroupDTO
{
    public Int64 idGroup { get; set; }
    public string GroupCode { get; set; }
    public string GroupDate { get; set; }
    public string Obs01 { get; set; }
    public string Obs02 { get; set; }
    public string Obs03 { get; set; }
    public string Obs04 { get; set; }
    public bool Active { get; set; }
    private List<ActionModelDTO> _Actions = new List<ActionModelDTO>();
    public List<ActionModelDTO> Actions { get { return _Actions; } set { _Actions = value; } }
}

public class ActionModelDTO
{
    public Int64 idAction { get; set; }
    public string ActionDate { get; set; }
    public Int32 Version { get; set; }
    public Int32 idGroup { get; set; }
    public bool Active { get; set; }
    private ActionDetailModelDTO _ActionDetailModel = new ActionDetailModelDTO();
    public ActionDetailModelDTO ActionDetailModel { get { return _ActionDetailModel; } set { _ActionDetailModel = value; } }
    private ActionUserDetailModelDTO _ActionUserDetailModel = new ActionUserDetailModelDTO();
    public ActionUserDetailModelDTO ActionUserDetailModel { get { return _ActionUserDetailModel; } set { _ActionUserDetailModel = value; } }
}

public class ActionDetailModelDTO
{
    public Int64 idActionDetailModel { get; set; }
    public Int32 idActionType { get; set; }
    public string ActionType { get; set; }
}

public class ActionUserDetailModelDTO
{
    public Int64 idActionUserDetailModel { get; set; }
    public string UserAccount { get; set; }
    public string UserObservation { get; set; }
    public string IdentificationNumber { get; set; }
}

映射如下:

  1. Action.Create.Request.operationdate => ActionModelDTO.ActionDate

  2. Action.Create.Request.action => ActionDetailModelDTO.ActionType

  3. Action.Create.Request.identificationaccount => ActionUserDetailModelDTO.UserAccount

  4. Action.Create.Request.observation => ActionUserDetailModelDTO.UserObservation

您必须映射到的对象是GroupDTO ...这是我第一次使用Automapper,它返回以下错误:

  

找到未映射的成员。在下面查看类型和成员。添加一个   自定义映射表达式,忽略,添加自定义解析器或修改   源/目标类型对于没有匹配的构造函数,请添加一个无参数   ctor,添加可选参数或映射所有构造函数参数

对象保留在数据库中之后,完成了我指定的映射中未出现的字段。

致谢!

0 个答案:

没有答案