Asp.net web api 映射到两种不同的集合类型时自动映射引发异常

Asp.net web api 映射到两种不同的集合类型时自动映射引发异常,asp.net-web-api,automapper-2,ablecommerce,Asp.net Web Api,Automapper 2,Ablecommerce,我正在编写一个ASP.NETWebAPI服务,它使用AutoMapper进行对象映射。我正在根据AbleCommerce购物车软件构建此服务。出现问题的服务(目的地)类跟踪用户及其地址 public class UserModel { public int UserID { get; set; } ... public List<AddressModel> Addresses { get; set; } } public class AddressModel

我正在编写一个ASP.NETWebAPI服务,它使用AutoMapper进行对象映射。我正在根据AbleCommerce购物车软件构建此服务。出现问题的服务(目的地)类跟踪用户及其地址

public class UserModel
{
    public int UserID { get; set; }
    ...
    public List<AddressModel> Addresses { get; set; }
}

public class AddressModel 
{
    public int AddressID { get; set; }
    public int UserID { get; set; }
    ...
    // Contains some of the fields from AbleCommerce Address
}
从AbleCommerce类到我的服务类的映射工作正常。只要忽略映射配置中不需要的属性,从服务类到AbleCommerce类的映射就可以工作。但是,对于Addresses属性,我不知道如何解决AbleCommerce
User
类中的
AddressCollection
和我的服务的
UserModel
类中的
List
之间的差异(从AutoMapper的角度)

当我测试映射配置时,得到的异常是:

AutoMapper.AutoMapperConfigurationException : The following property on AlfredModel.Models.Classes.AddressModel cannot be mapped: 
    Addresses
Add a custom mapping expression, ignore, add a custom resolver, or modify the destination type AlfredModel.Models.Classes.AddressModel.
Context:
    Mapping to property Addresses from System.Object to AlfredModel.Models.Classes.AddressModel
    Mapping to property Addresses from CommerceBuilder.Users.AddressCollection to System.Collections.Generic.List`1[[AlfredModel.Models.Classes.AddressModel, AlfredModel, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]
    Mapping to property User from CommerceBuilder.Users.User to AlfredModel.Models.Classes.UserModel
    Mapping from type CommerceBuilder.Orders.Basket to AlfredModel.Models.Classes.BasketModel
Exception of type 'AutoMapper.AutoMapperConfigurationException' was thrown.
   at AutoMapper.ConfigurationStore.DryRunTypeMap(ICollection`1 typeMapsChecked, ResolutionContext context)
   at AutoMapper.ConfigurationStore.DryRunTypeMap(ICollection`1 typeMapsChecked, ResolutionContext context)
   at AutoMapper.ConfigurationStore.DryRunTypeMap(ICollection`1 typeMapsChecked, ResolutionContext context)
   at AutoMapper.ConfigurationStore.DryRunTypeMap(ICollection`1 typeMapsChecked, ResolutionContext context)
   at AutoMapper.ConfigurationStore.AssertConfigurationIsValid(IEnumerable`1 typeMaps)
   at AutoMapper.ConfigurationStore.AssertConfigurationIsValid()
   at AutoMapper.Mapper.AssertConfigurationIsValid()
   at AlfredModelTests.AlfredMapConfigurationTest.Maps_Configured_Correctly() in C:\Users\ntruick\Documents\Visual Studio 2010\Projects\AlfredModel\AlfredModelTests\AlfredMapConfigurationTest.cs:line 21

这让我感到困惑,因为
AddressModel
类没有名为
Addresses
的属性。我显然错过了什么。如有任何建议、建议或澄清,将不胜感激

我相信我已经找到了解决办法。经过大量的挖掘(以及几次赞赏的投票),我发现我需要通过为用户地址集合创建一个类型转换器来定义映射:

public class UserAddressesTypeConverter : 
    ITypeConverter<PersistentCollection<Address>, List<AddressModel>
{
    var source = (PersistentCollection<Address>)context.SourceValue;
    var result = new List<AddressModel>();

    foreach (Address address in source.Cast<Address>().ToList()
        result.Add(Mapper.Map<AddressModel>(address));

    return result;
}
公共类用户地址类型转换器:
IT类型转换器
public class UserAddressesTypeConverter : 
    ITypeConverter<PersistentCollection<Address>, List<AddressModel>
{
    var source = (PersistentCollection<Address>)context.SourceValue;
    var result = new List<AddressModel>();

    foreach (Address address in source.Cast<Address>().ToList()
        result.Add(Mapper.Map<AddressModel>(address));

    return result;
}