gpt4 book ai didi

c# - AutoMapper.dll 中出现“AutoMapper.AutoMapperMappingException”

转载 作者:太空狗 更新时间:2023-10-29 21:12:38 26 4
gpt4 key购买 nike

 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 具有完全相同数量的 Prop ,下面是我正在使用的代码及其抛出错误而没有获得太多信息,我是什么这里不见了?

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

最佳答案

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

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

Working demo on dotnetfiddle .

关于c# - AutoMapper.dll 中出现“AutoMapper.AutoMapperMappingException”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28259216/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com