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

时间:2020-05-21 22:40:00

标签: c# asp.net-core automapper

我正在尝试将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; }
    }


所以我想只从位置获取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,没有帮助

1 个答案:

答案 0 :(得分:1)

您创建了从LocationUpdatedLocation的地图,

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

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