【问题标题】:AutoMapperMapping: Missing type map configuration or unsupported mappingAutoMapperMapping:缺少类型映射配置或不支持的映射
【发布时间】:2021-08-13 16:17:51
【问题描述】:

我在过去 2 天尝试使用 aspnetboilerplate 创建简单的 INNER JOIN,而我m getting error that I really dont 知道如何处理。 我的观点是,我需要在 ObjectMapper.Map 中添加两个 DTO

我的代码:

StudentDto

[AutoMap(typeof(Student))]
public class Student: EntityDto
{

    public string Name { get; set; }
    public int Last_Name{ get; set; }
    public int City_Id { get; set; }
}
} 

CityDto

[AutoMap(typeof(City))]
public class CityDto : EntityDto
{
    public string Name{ get; set; }
    public string Address{ get; set; }
}

StudentAppService.cs

 private readonly IRepository<Student> _studentRepository;
   private readonly IRepository<City> _cityRepository;


    public StudentAppService(IRepository<Student> _studentRepository, IRepository<City> _cityRepository)
    {
         _studentRepository = studentRepository ;
       _cityRepository = cityRepository ;
     }

    public List<StudentDto> GetStudentWithCityName()
    {
        var data = (from s in _studentRepository.GetAll() join c in _cityRepository.GetAll() on s.City_Id equals c.Id select new { name = s.Name }).ToList();
        return ObjectMapper.Map<List<StudentDto>>(data);

    }

当我构建应用程序并尝试使用 SWAGGER 执行 GET 时,我收到此错误(带有我想要的数据值)

谁能帮我解决这个问题?

【问题讨论】:

  • 您从 repo 中选择了一个匿名对象 (new { ... }),而不是 Student,但配置了 StudentDto 和 Student 之间的映射

标签: c# asp.net-core aspnetboilerplate


【解决方案1】:

当你做的时候

"选择新的 { name = s.Name }"

对象不能是匿名的,必须是存在且具有AutoMap属性的对象,才能对其进行映射,也可以直接选择StudentDto对象。

【讨论】:

    【解决方案2】:

    你确定 prop 'Last_Name' 的 dataType 是 'int' 吗?它不应该是一个字符串?

    【讨论】:

      猜你喜欢
      • 2015-06-27
      • 1970-01-01
      • 2019-03-01
      • 2017-04-16
      • 2015-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-26
      相关资源
      最近更新 更多