Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'AutoMapper.AutoMapperMappingException' occurred in AutoMapper.dll

 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

like image 394
Nick Kahn Avatar asked Feb 01 '15 03:02

Nick Kahn


1 Answers

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.

like image 163
aloisdg Avatar answered Sep 19 '22 08:09

aloisdg