2015-10-23 61 views
2

下面是我在我的WPF應用程序:AutoMapper未映...沒有錯誤,則拋出

public static class MappingCreator 
{ 
    public static void CreateMaps() 
    { 
     Mapper.CreateMap<SO.Services.Data.ServiceModel.Types.Customer, Customer>(); 
     Mapper.CreateMap<List<SO.Services.Data.ServiceModel.Types.CustomerSearchResult>, List<CustomerSearchResult>>(); 

     Mapper.AssertConfigurationIsValid(); 
    } 
} 

CreateMaps()是在應用程序啓動調用一次。

DTO:

namespace SO.Services.Data.ServiceModel.Types 
{ 
    [DataContract] 
    public class CustomerSearchResult 
    { 
     [DataMember] 
     public int CustomerId { get; set; } 
     [DataMember] 
     public string AccountType { get; set; } 
     [DataMember] 
     public string ShortName { get; set; } 
     [DataMember] 
     public string LegacyName { get; set; } 
     [DataMember] 
     public string LegacyContactName { get; set; } 
     [DataMember] 
     public string City { get; set; } 
     [DataMember] 
     public string StateAbbreviation { get; set; } 
     [DataMember] 
     public string Country { get; set; } 
     [DataMember] 
     public string PostalCode { get; set; } 
    } 
} 

型號:

namespace SO.Models 
{ 
    public class CustomerSearchResult : BindableBase 
    { 
     public int CustomerId { get; set; } 
     public string AccountType { get; set; } 
     public string ShortName { get; set; } 
     public string LegacyName { get; set; } 
     public string LegacyContactName { get; set; } 
     public string City { get; set; } 
     public string StateAbbreviation { get; set; } 
     public string Country { get; set; } 
     public string PostalCode { get; set; } 
    } 
} 

擴展方法:

public static class DtoMappingExtensions 
{ 
    public static List<CustomerSearchResult> ToModels(this List<SO.Services.Data.ServiceModel.Types.CustomerSearchResult> customerSearchList) 
    { 
     return Mapper.Map<List<SO.Services.Data.ServiceModel.Types.CustomerSearchResult>, List<CustomerSearchResult>>(customerSearchList); 
    } 
} 

我調用,它返回一個List<SO.Services.Data.ServiceModel.Types.CustomerSearchResult>一個servicestack服務......當我用ToModels擴展方法,即使源代碼爲lis,它也會返回一個帶有0條記錄的List t有25k左右的記錄。

我很難過。

+1

我不認爲你需要的清單映射。只需爲單個實體對象創建映射(並執行.ReverseMap()),如果需要它們兩種方式。當你做Mapper.Map時,你指定列表,然後列表(源)。 – tc44

+0

是的......那樣做......一定是對Automapper的更新?一些其他類似的問題說,做我做的。謝謝。把你在回答中說的話,我會接受。 –

回答

5

在你CreateMaps(取代以前的語句),你會指定對象的映射,不在名單映射。

Mapper.CreateMap<SO.Services.Data.ServiceModel.Types.CustomerSearchResult, CustomerSearchResult>().ReverseMap(); 

,然後在ToModels()你做

Mapper.Map<List<CustomerSearchResult>, List<SO.Services.Data.ServiceModel.Types.CustomerSearchResult>>(customerSearchList); 
1

我認爲問題在這一行。

Mapper.CreateMap<List<SO.Services.Data.ServiceModel.Types.CustomerSearchResult>, List<CustomerSearchResult>>(); 

這條語句沒有必要,因爲AutoMapper會在您映射類時自動處理列表映射。

我想你應該與這一個

Mapper.CreateMap<SO.Services.Data.ServiceModel.Types.CustomerSearchResult, CustomerSearchResult>();