我正在编写一个使用 AutoMapper 进行对象映射的 ASP.NET Web API 服务。我正在针对 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 包中的User
和Address
类。我已经包含了祖先类的类声明,因为我相信使用泛型约束可能存在问题,但我并不完全确定。
public class User: IPersistable
{
public int UserId { get; set; }
...
public AddressCollection Addresses
{
get
{
// Calls an internal method which creates a new instance
// of AddressCollection and loads the addresses for the
// user from the database.
...
return _Addresses;
}
}
}
public class Address : IPersistable
{
public int AddressId { get; set; }
public int UserId { get; set; }
...
}
public class AddressCollection : PersistentCollection<Address> { ... }
public class PersistentCollection<T> : SortableCollection<T> where T : IPersistable { ... }
public interface IPersistable { ... }
从 AbleCommerce 类到我的服务类的映射按预期工作。只要我忽略映射配置中不需要的属性,从我的服务类映射到 AbleCommerce 类就可以了。但是,对于 Addresses 属性,我不知道如何解决AddressCollection
AbleCommerceUser
类和List<AddressModel>
我的服务UserModel
类之间的差异(从 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
. 我显然错过了一些东西。任何建议、建议或澄清将不胜感激。