c# – AutoMapper.dll中出现’AutoMapper.AutoMapperMappingExce
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; } } 编辑:
我的客户& ClientViewModel具有完全相同数量的道具,下面是我正在使用的代码及其抛出错误而没有获得太多信息,我在这里缺少什么? Client client = context.Clients.Where(x => x.CustomerID == id).FirstOrDefault(); ClientViewModel clientViewModel = Mapper.Map<Client,ClientViewModel>(client);
解决方法
你忘了创建你的地图.将其添加到您的代码中(在调用Mapper类之前):
Mapper.CreateMap<Client,ClientViewModel>(); ClientViewModel cvm = Mapper.Map<Client,ClientViewModel>(client); Working demo on dotnetfiddle. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |