K.Z
K.Z

Reputation: 5075

How to map class with multiple custom property type using Auto-Mapper

I am using auto-mapper to map class object of standard data types i.e. string, int which is working fine but now I have class with multiple custom types and I am struggling to map it to identical class object.

Source

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

public Web_EBS_SyncEntity EBS_Sync { get; set; }

public Web_SyncAuditLogEntity SyncAuditLog { get; set; }
}

Destination

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

Destination Object structure expecting

enter image description here

Error

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

Mapping class where I am getting error

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

Upvotes: 0

Views: 782

Answers (1)

Mauro Sampietro
Mauro Sampietro

Reputation: 2814

Config like this:

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

Upvotes: 1

Related Questions