具有嵌套多个类对象的自动映射器类对象

时间:2016-12-22 13:50:50

标签: c# automapper

我使用auto-mapper来映射单个类对象,它正常工作,例如

class_source{
 string a;
 int b
}

class_destination
{
 string a;
 int b
}

现在我将viewModel基于多个类对象作为

来源

public class WebSyncSummaryEntity
{
    public Web_AppFormsEntity AppForms { get; set; }

    public Web_EBS_SyncEntity EBS_Sync { get; set; }

    public Web_SyncAuditLogEntity SyncAuditLog { get; set; }
}

目标

[DataContract]
public class WebSyncSummaryView
{
    [DataMember]
    public Web_AppForms AppForms { get; set; }

    [DataMember]
    public Web_EBS_Sync EBS_Sync { get; set; }

    [DataMember]
    public Web_SyncAuditLog SyncAuditLog { get; set; }
}

我如何映射到等效的类对象

我尝试过以下代码来映射但错误

public IList<WebSyncSummaryView> GetWebSyncSummary()
{
    IList<WebSyncSummaryView> _WebSyncSummaryView = null;

    IList<WebSyncSummaryEntity> _WebSyncSummaryEntity = _WebSyncCoreObject.GetWebSyncSummary();


    if (_WebSyncSummaryEntity != null)
    {
        var config = new MapperConfiguration(cfg =>
        {
            cfg.CreateMap<WebSyncSummaryEntity, WebSyncSummaryView>();
        });

        IMapper mapper = config.CreateMapper();

        _WebSyncSummaryView = mapper.Map<IList<WebSyncSummaryEntity>, IList<WebSyncSummaryView>>(_WebSyncSummaryEntity);
    }

    return _WebSyncSummaryView;
}

错误

 "Error mapping types.\r\n\r\nMapping types:\r\nWebSyncSummaryEntity ->  WebSyncSummaryView\r\nApp.Entities.WebSyncSummaryEntity -> App.WebServices.DataContract.WebSyncSummaryView\r\n\r\nType Map configuration:\r\nWebSyncSummaryEntity -> WebSyncSummaryView\r\nApp.Entities.WebSyncSummaryEntity -> App.WebServices.DataContract.WebSyncSummaryView\r\n\r\nProperty:\r\nSyncAuditLog"}

映射类型时出错。

Mapping types:
IList`1 -> IList`1
System.Collections.Generic.IList`1[[App.Entities.WebSyncSummaryEntity,  App.Entities, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] ->  System.Collections.Generic.IList`1[[App.WebServices.DataContract.WebSyncSummaryView, App.Services.Contracts, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]

2 个答案:

答案 0 :(得分:0)

更改行

_WebSyncSummaryView = mapper.Map<IList<WebSyncSummaryEntity>, IList<WebSyncSummaryView>>(_WebSyncSummaryEntity);

_WebSyncSummaryView = mapper.Map<IList<WebSyncSummaryView>>(_WebSyncSummaryEntity);

无需告知AutoMapper源类型。您尝试映射IList&lt;&gt;,但您只想映射WebSyncSummaryEntity。只需告诉Mapper您想要的结果是什么,其余来自AutoMapper的伏都教;)

答案 1 :(得分:0)

找到答案

 public IList<WebSyncSummaryView> GetWebSyncSummary()
    {
        IList<WebSyncSummaryView> _WebSyncSummaryView = null;

        IList<WebSyncSummaryEntity> _WebSyncSummaryEntity = _WebSyncCoreObject.GetWebSyncSummary();


        if (_WebSyncSummaryEntity != null)
        {
            var config = new MapperConfiguration(cfg =>
            {
                cfg.CreateMissingTypeMaps = true; //............... missing this
                cfg.CreateMap<WebSyncSummaryEntity, WebSyncSummaryView>();
            });

            IMapper mapper = config.CreateMapper();

            _WebSyncSummaryView = mapper.Map<IList<WebSyncSummaryEntity>, IList<WebSyncSummaryView>>(_WebSyncSummaryEntity);


        }