微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐