'AutoMapper.AutoMapperMappingException' 发生在 AutoMapper.dll

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

编辑:

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

我的 ClientClientViewModel 具有完全相同数量的道具,下面是我正在使用的代码及其抛出错误而没有获得太多信息,我在这里缺少什么?

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

您刚刚忘记创建地图。将此添加到您的代码中(在调用 Mapper class 之前):

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

Working demo on dotnetfiddle.

在调用地图之前。您需要调用 CreateMap:

Mapper.CreateMap<Client, ClientViewModel>();

通常您会在应用程序初始化 code/class 中调用它,例如在 global.asax.cs 中。