自动翻车人翻双失败?财产



我使用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类都有两个属性,分别叫做Longitude和Latitude。这些属性(在两个类中)被定义为double?如果我注释掉这些属性,一切都会正常工作。如果我把所有这些属性都变成两倍,那么一切都没问题。只有两倍?这就是问题所在。

我使用AutoMapper 2.2.1通过NuGet下载。

我在其他帖子中读到,这个问题与可空性应该是固定的。这让我相信我可能在做一些不同的事情,所以我要发布我的代码,看看是否有人可以看到可能是一个问题:

域模型

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; }
} 
<<p> 视图模型/strong>
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);

有没有其他人遇到过这种情况或找到了解决方案?

 Mapper.CreateMap<AccountAddress, AddressModel>();

改为

Mapper.CreateMap<Address, AddressModel>();

这是因为您正在将Address类映射到AddressModel类。

相关内容

  • 没有找到相关文章

最新更新