【发布时间】:2021-03-10 09:29:36
【问题描述】:
我正在尝试使用 Automapper 展平 json 结果,但我收到一条错误消息:Missing type map configuration or unsupported mapping
在我的项目中,我安装了 AutoMapper 和 AutoMapper 依赖注入包。
我的 Startup.cs 有以下调用:
public void ConfigureServices(IServiceCollection services)
{
services.AddAutoMapper(typeof(Startup));
}
我已按如下方式设置我的映射配置文件:
public class MappingProfile : Profile
{
public MappingProfile()
{
CreateMap<JiraTicket, JiraDto>();
}
}
我想要展平的模型和 DTO 应该展平为:
public class JiraTicket
{
[Key]
public int TicketId { get; set; }
public string id { get; set; }
public string self { get; set; }
public string key { get; set; }
public JiraTicketFields fields { get; set; }
}
public class JiraDto
{
public int Id { get; set; }
public string Creator { get; set; }
public string Description { get; set; }
public string Key { get; set; }
public string Self { get; set; }
}
大部分 DTO 变量都在 `JiraTicketFields 中。
最后,我使用以下代码行调用我的仓库:
public async Task<IEnumerable<JiraDto>> GetAll()
{
var tickets = _jiraRepo.GetAll().ToList();
var result = (IEnumerable<JiraDto>)_mapper.Map<JiraDto>(tickets);
return result;
}
当我的项目到达var result... 行时,就会引发错误。我错过了什么?
【问题讨论】:
-
您的 MappingProfile 和 Startup 是否在同一个程序集中?
标签: c# .net-core entity-framework-core automapper