【问题标题】:c# Automapper - Missing type map configuration or unsupported mappingc# Automapper - 缺少类型映射配置或不支持的映射
【发布时间】:2020-05-21 22:40:00
【问题描述】:

我正在尝试将 DTO 映射到响应对象,但由于 DTO 的一个子对象,我不断收到此错误:

AutoMapper.AutoMapperMappingException: Error mapping types.

Mapping types:

Post -> UpdatePostResponse

BingoAPI.Models.Post -> Bingo.Contracts.V1.Responses.Post.UpdatePostResponse

Type Map configuration:

Post -> UpdatePostResponse

BingoAPI.Models.Post -> Bingo.Contracts.V1.Responses.Post.UpdatePostResponse

Destination Member:

Location

 ---> AutoMapper.AutoMapperMappingException: Missing type map configuration or unsupported mapping.

Mapping types:

Location -> Location

BingoAPI.Models.Location -> Bingo.Contracts.V1.Responses.Post.Location

   at lambda_method(Closure , Location , Location , ResolutionContext )

   at lambda_method(Closure , Post , UpdatePostResponse , ResolutionContext )

   --- End of inner exception stack trace ---

这就是我要映射的内容:

public class Location
    {
        public int Id { get; set; }

        public double? Logitude { get; set; }

        public double? Latitude { get; set; }

        public string? Address { get; set; }

        public string? City { get; set; }

        public string? Region { get; set; }

        public string? Country { get; set; }

        public Post Post { get; set; }

        public int PostId { get; set; }
   }


对此UpdatePostResponse.UpdatedLocation

public class UpdatePostResponse
    {
        public int Id { get; set; }

        public Int64 PostTime { get; set; }

        public Int64 EventTime { get; set; }

        public Location Location { get; set; }

        public string UserId { get; set; }

        public Event Event { get; set; }

        public IEnumerable<string>? Pictures { get; set; }

        public List<String>? Tags { get; set; }
    }
    public class UpdatedEvent
    {
        public int Id { get; set; }
    }
    public class UpdatedLocation
    {
        public int Id { get; set; }

        public double? Logitude { get; set; }

        public double? Latitude { get; set; }
    }


所以我试图仅从 Location

获取 Id、经度、纬度值 这就是我定义映射的方式:

CreateMap<Models.Location, UpdatedLocation>()
                .ForMember(dest => dest.Id, opt => opt.MapFrom(s => s.Id))
                .ForMember(dest => dest.Latitude, opt => opt.MapFrom(s => s.Latitude))
                .ForMember(dest => dest.Logitude, opt => opt.MapFrom(s => s.Logitude));

CreateMap<Post, UpdatePostResponse>()
                .ForMember(dest => dest.Location, opt => opt.MapFrom(s => s.Location))
                .ForMember(dest => dest.Event, opt => opt.MapFrom(s => s.Event));

还有映射它们的代码

var ok = new Response<UpdatePostResponse>(mapper.Map<UpdatePostResponse>(mappedPost));

我也考虑过这个solution,没有帮助

【问题讨论】:

  • 你能告诉我们执行映射的代码吗?
  • @CiaranGallagher 我用代码更新了帖子
  • 消息说您缺少地图,BingoAPI.Models.Location -> Bingo.Contracts.V1.Responses.Post.Location。
  • 你的Post 结构是什么? Location中的Post字段映射到UpdatePostResponse需要哪个字段?作为以下帖子答案中的@kebek,您需要修改 UpdatePostResponse。对于Event 类和Post 类也是如此。您首先需要明确Location中的哪些字段映射到UpdatePostResponse中的哪些字段,然后我们可以重现并尝试为您解决问题。

标签: c# asp.net-core automapper


【解决方案1】:

您创建了一张从LocationUpdatedLocation 的地图,

但在UpdatePostResponse 类中(与您一起映射您的帖子),有一个Loction 类型的位置属性:

    public class UpdatePostResponse
    {
        public int Id { get; set; }
        public Int64 PostTime { get; set; }
        public Int64 EventTime { get; set; }
        public Location Location { get; set; }
        public string UserId { get; set; }
        public Event Event { get; set; }
        public IEnumerable<string>? Pictures { get; set; }
        public List<String>? Tags { get; set; }
    }

请尝试以下类定义:

    public class UpdatePostResponse
    {
        public int Id { get; set; }
        public Int64 PostTime { get; set; }
        public Int64 EventTime { get; set; }
        public UpdatedLocation Location { get; set; } // notice the type
        public string UserId { get; set; }
        public Event Event { get; set; }
        public IEnumerable<string>? Pictures { get; set; }
        public List<String>? Tags { get; set; }
    }

【讨论】:

    猜你喜欢
    • 2019-03-01
    • 2017-04-16
    • 2015-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-15
    • 2015-05-07
    • 1970-01-01
    相关资源
    最近更新 更多