如何使用Auto-Mapper映射具有多个自定义属性类型的类

时间:2016-12-23 10:14:08

标签: c# .net automapper

我使用自动映射器来映射标准数据类型的类对象,即string,int,它工作正常,但现在我有多个自定义类的类,我正在努力将它映射到相同的类对象。

来源

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

期望

的目标对象结构

enter image description here

错误

"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]]

我遇到错误的映射类

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

1 个答案:

答案 0 :(得分:1)

这样的配置:

var config = new MapperConfiguration(cfg =>
{
    cfg.CreateMissingTypeMaps = true; //your are missing this
    cfg.CreateMap<WebSyncSummaryEntity, WebSyncSummaryView>();
});