AutoMapper 未映射...未抛出错误

AutoMapper Not Mapping... No Errors Thrown

这是我的 WPF 应用程序中的内容:

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

我调用一个 servicestack 服务,其中 returns 一个 List<SO.Services.Data.ServiceModel.Types.CustomerSearchResult> ... 当我使用 ToModels 扩展方法时,它 returns 一个包含 0 条记录的列表,即使源列表有 25k 左右的记录。

我被难住了。

我认为问题出在这一行。

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

这个语句没有必要,因为当你映射类时,AutoMapper会自动处理列表的映射。

我认为你应该用这个替换之前的声明

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

在您的 CreateMaps() 中,您将指定对象映射,而不是列表映射。

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

然后在您的 ToModels() 中执行

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