c# Automapper - 缺少类型映射配置或不支持的映射

c# Automapper - Missing type map configuration or unsupported mapping

我正在尝试将 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,没用

您创建了从 LocationUpdatedLocation

的地图

但在 UpdatePostResponse class(与您映射您的帖子)中,有一个 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; }
    }

请尝试以下 class 定义:

    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; }
    }