'AutoMapper.AutoMapperMappingException' occurred in AutoMapper.dll

29,207

Solution 1

You just forgot to create your map. Add this to your code (before the call of the Mapper class) :

Mapper.CreateMap<Client, ClientViewModel>();
ClientViewModel cvm = Mapper.Map<Client, ClientViewModel>(client);

Working demo on dotnetfiddle.

Solution 2

Before calling Map. You need to call CreateMap:

Mapper.CreateMap<Client, ClientViewModel>();

Generally you would call this in your application initialization code/class, in global.asax.cs for example.

Share:
29,207
Nick Kahn
Author by

Nick Kahn

Updated on July 28, 2022

Comments

  • Nick Kahn
    Nick Kahn almost 2 years
     public class ClientViewModel
        {
            [Required(ErrorMessage = "The Client Code field is required.")]  
            public string ClientCode { get; set; }
            [Required(ErrorMessage = "The Company Legal Name field is required.")]  
            public string CompanyLegalName { get; set; }
            public string Notes { get; set; }
            public string Address { get; set; }
            public string City { get; set; }
            public string State { get; set; }
            public string Zip { get; set; }
            public Nullable<DateTime> ScheduledDate { get; set; }
            public Nullable<decimal> AmountDiscount { get; set; }
        }
    
        public class Client
        {
            public string ClientCode { get; set; }   
            public string CompanyLegalName { get; set; }
            public string Notes { get; set; }
            public string Address { get; set; }
            public string City { get; set; }
            public string State { get; set; }
            public string Zip { get; set; }
            public Nullable<DateTime> ScheduledDate { get; set; }
            public Nullable<decimal> AmountDiscount { get; set; }
        }
    

    Edit:

    Exception Details: AutoMapper.AutoMapperMappingException: Missing type map configuration or unsupported mapping.

    Mapping types: Client -> ClientViewModel myapp.Models.Client -> myapp.Models.ClientViewModel

    Destination path: ClientViewModel

    Source value: myapp.Models.Client

    My Client & ClientViewModel has exact same number of props and below is my code that I'm using and its throwing error without getting much information, what I'm missing here?

    Client client = context.Clients.Where(x => x.CustomerID == id).FirstOrDefault();
    ClientViewModel clientViewModel = Mapper.Map<Client, ClientViewModel>(client);
    

    An exception of type 'AutoMapper.AutoMapperMappingException' occurred in AutoMapper.dll but was not handled in user code