加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – AutoMapper.dll中出现’AutoMapper.AutoMapperMappingExce

发布时间:2020-12-16 02:04:29 所属栏目:百科 来源:网络整理
导读: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 { g
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

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

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类之前):

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

Working demo on dotnetfiddle.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读