2014-01-17 76 views
0

我現在對automapper有點麻煩。Automapper將實體扁平化爲dto

我有以下屬性的牽頭實體

Id as long 

FirstName as string 

LastName as string 

Title as string 

LeadOpportunities as ICollection(Of ILeadOpportunity) 

LeadOpportunity包含以下屬性

SourceDate as datetime 

LeadOpportunityType as LeadOpportunityType 

LeadOpportunityType包含以下屬性

Description as string 

我有一個LeadSearchDTO其中有以下屬性

LeadId as long 

LastActionDate as datetime 

Title as string 

FirstName as string 

LastName as string 

Type as string 

我在我的引導程序類中有以下內容。

Mapper.CreateMap(Of List(Of ILead), List(Of ILeadSearchDTO))() 

Mapper.CreateMap(Of ILead, ILeadSearchDTO)().ForMember(Function(en) en.LeadId, Sub(map) map.MapFrom(Function(dto) dto.Id)) _ 
                .ForMember(Function(en) en.LastActionDate, Sub(map) map.MapFrom(Function(dto) dto.LeadOpportunities.FirstOrDefault().SourceDate)) _ 
                .ForMember(Function(en) en.Type, Sub(map) map.MapFrom(Function(dto) dto.LeadOpportunities.FirstOrDefault().LeadOpportunityType.Description)) 

然後,我使用下面的代碼來映​​射這兩個對象。

response.LeadDTOs = Me._mapper.Map(Of List(Of ILead), List(Of ILeadSearchDTO))(leads) 

當我把我的斷點放在response.LeadDTOs上時,它沒有映射到Leads集合中。

有沒有人有我可能會失蹤的想法?我剛剛剛開始使用automapper一個多星期,並且我剛剛使用了簡單的轉換。

編輯:

如果我離開了Mapper.CreateMap(名單(共i引線)中,列表(中ILeadSearchDTO))(),它會給我下面的錯誤信息。

Mapping types: 
ILead -> ILeadSearchDTO 
_8T.DataHub.Model.Interfaces.Entities.Lead.ILead -> _8T.DataHub.Service.Interfaces.DTOs.Lead.ILeadSearchDTO 

Destination path: 
List`1[0] 

Source value: 
_8T.DataHub.Model.Implementation.Entities.Lead.Lead 

我已經有了Mapper.AssertConfigurationIsValid()。

+0

您必須映射到具體類,AutoMapper無法實現接口。 –

回答

0

我終於設法解決它。我不得不使用下面的代碼。

ConstructUsing(Function(x As ResolutionContext) New LeadSearchDTO()) 
+0

不,你應該做'Mapper.CreateMap(ILead,LeadSearchDTO)'(沒有接口作爲目標類型)。 –

0

我覺得你不需要調用

Mapper.CreateMap(Of List(Of ILead), List(Of ILeadSearchDTO))() 

Automapper會自動處理名單。但無論如何,儘管打電話

Mapper.AssertConfigurationIsValid(); 

在最後CreateMap

+0

我已經有了Mapper.AssertConfigurationIsValid()。 – user3177251