Nick Kahn
Nick Kahn

Reputation: 20078

'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

Upvotes: 11

Views: 35806

Answers (2)

aloisdg
aloisdg

Reputation: 23521

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.

Upvotes: 15

Noel
Noel

Reputation: 587

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.

Upvotes: 6

Related Questions