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.

相关文章

目录简介使用JS互操作使用ClipLazor库创建项目使用方法简单测...
目录简介快速入门安装 NuGet 包实体类User数据库类DbFactory...
本文实现一个简单的配置类,原理比较简单,适用于一些小型项...
C#中Description特性主要用于枚举和属性,方法比较简单,记录...
[TOC] # 原理简介 本文参考[C#/WPF/WinForm/程序实现软件开机...
目录简介获取 HTML 文档解析 HTML 文档测试补充:使用 CSS 选...