我正在使用 AutoMapper 将域对象映射到控制器中的 ViewModel 对象。一切正常,直到我尝试添加双倍?特性。我已经开始收到以下错误:
Missing type map configuration or unsupported mapping.
Mapping types:
Address -> AddressModel
Domain.Address -> Web.Models.AddressModel
Destination path:
AccountAddressModel.Address.Address
Source value:
Domain.Address
我的 Address 类和 AddressModel 类都有两个属性,称为经度和纬度。这些属性(在两个类中)被定义为 double?。如果我注释掉这些属性,一切正常。如果我将所有这些属性简单地加倍,那么一切正常。只是双倍?这导致了问题。
我正在使用通过 NuGet 下载的AutoMapper 2.2.1 。
我在其他帖子中读到,这个 nullables 问题应该得到解决。这让我相信我可能会做一些不同的事情,所以我将发布我的代码,看看是否有人能看到可能是问题的东西:
领域模型
public class AccountAddress
{
public int Id { get; set; }
public int AccountId { get; set; }
public int AddressId { get; set; }
public Address Address { get; set; }
...
}
public class Address : IUserTrackingEntity
{
public int Id { get; set; }
public string Addressee { get; set; }
public string Street1 { get; set; }
public string Street2 { get; set; }
...
public double? Longitude { get; set; }
public double? Latitutde { get; set; }
}
查看模型
public class AccountAddressEditModel
{
public string AccountName { get; set; }
public AccountAddressModel Address { get; set; }
public IList<Country> CountriesList { get; set; }
...
}
public class AccountAddressModel
{
public AccountAddressModel()
{
Address = new AddressModel();
}
public int AccountId { get; set; }
public int AddressId { get; set; }
public int Id { get; set; }
public AddressModel Address { get; set; }
}
public class AddressModel
{
public int Id { get; set; }
public string Addressee { get; set; }
[Required(ErrorMessage="A street address is required.")]
public string Street1 { get; set; }
public string Street2 { get; set; }
...
public double? Longitude { get; set; }
public double? Latitude { get; set; }
}
在我的控制器中映射代码
//Get an AccountAddress
address = _context.AccountAddresses.SingleOrDefault(ad => ad.Id == 12345);
model = new AccountAddressEditModel();
Mapper.CreateMap<AccountAddress, AccountAddressModel>();
Mapper.CreateMap<AccountAddress, AddressModel>();
Mapper.Map(address, model.AccountAddress);
有没有其他人经历过这种情况或找到解决方案?