Question

I'm using AutoMapper to map Domain objects to ViewModel objects in my controller. Everything was working fine until I tried adding double? properties. I've started getting the following error:

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

My Address class and AddressModel class both have two properties called Longitude and Latitude. These properties (in both classes) are defined as double?. If I comment out these properties, everything works fine. If I make all of these properties simply double, then everything works fine. It's only double? that causes the problem.

I'm using AutoMapper 2.2.1 downloaded via NuGet.

I've read in other posts that this problem with nullables was supposed to be fixed. This leads me to believe I might be doing something different so I'm going to post my code to see if anyone can see something that might be an issue:

DOMAIN MODELS

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

VIEW MODELS

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

MAPPING CODE IN MY CONTROLLER

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

Has anyone else experienced this or found a solution for this?

Était-ce utile?

La solution

The line

 Mapper.CreateMap<AccountAddress, AddressModel>();

should be changed to

Mapper.CreateMap<Address, AddressModel>();

This because you are mapping the Address class to the AddressModel class.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top