我的代码中显示缺少类型映射配置异常

Missing type map configuration exception is showing in my code

我在控制器中有以下代码并显示异常。

[HttpGet("{id}")]
    public IActionResult GetCategoryGoalsById(int id)
    {
        try
        {
            var categories = _unitOfWork.Category.GetCategoryByGoalId(id);
            if (categories == null)
            {
                _loggerManager.LogError($"Category with id: {id}, hasn't been found in db.");
                return NotFound();
            }
            else
            {
                _loggerManager.LogInfo($"Returned category with id: {id}");
                var categoryResult = _mapper.Map<CategoryDetailVm>(categories);
                return Ok(categoryResult);
            }
        }
        catch (Exception ex)
        {
            _loggerManager.LogError($"Something went wrong inside categoryResult action: {ex.Message}");
            return StatusCode(500, "Internal server error");
        }
    }

实体在哪里class是这样的:

public class Category 
{
    public int Id { get; set; }
    public DateTime CreatedAt { get; set; }
    public DateTime UpdatedAt { get; set; }
    public string CategoryName { get; set; }

    [ForeignKey(nameof(Goals))]
    public int GoalId { get; set; } 
    public Goals Goals { get; set; }

}

和 vm class 模块 class 写为:

public class CategoryDetailVm
{
    public int Id { get; set; }
    public string CategoryName { get; set; }
    
}

代码使用 UnitofWork 以存储库模式编写,存储库部分编写为:

public IEnumerable<Category> GetCategoryByGoalId(int goalId)
    {
        return FindByCondition(g => g.Goals.Id.Equals(goalId)).ToList();
    }

此处显示以下异常,我该如何解决以下问题:

  • ex  {"Missing type map configuration or unsupported mapping.\r\n\r\nMapping types:\r\nObject ->
    

CategoryDetailVm\r\nSystem.Object -> EthosAPI.ViewModelEntities.CategoryDetailVm"} System.Exception {AutoMapper.AutoMapperMappingException}

您似乎缺少自动映射器映射,您添加了吗? https://docs.automapper.org/en/stable/Getting-started.html#how-do-i-use-automapper

例如。 var config = new MapperConfiguration(cfg => cfg.CreateMap<CategoryDetailVm, Categorie>());

您还要将一个对象映射到整个列表,因此您还应该有一个列表映射,请参阅: https://docs.automapper.org/en/stable/Lists-and-arrays.html

所以 var categoryResult = _mapper.Map<CategoryDetailVm>(categories); 应该更像 var categoryResult = _mapper.Map<IEnumerable<CategoryDetailVm>>(categories); 之类的。