簡體   English   中英

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

[英]c# Automapper - Missing type map configuration or unsupported mapping

我正在嘗試 map 對響應 object 的 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 ---

這就是我要嘗試的 map:

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、Longitude、Latitude 值

這就是我定義映射的方式:

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

我也考慮過這個解決方案,沒有幫助

您創建了一個 map 從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; }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM