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

问题描述 投票:0回答:1

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

c# asp.net-core automapper
1个回答
0
投票

您创建了从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; }
    }
© www.soinside.com 2019 - 2024. All rights reserved.