缺少类型映射配置或不受支持的映射。用于DTO的收集

时间:2019-08-01 17:48:00

标签: asp.net-core asp.net-web-api automapper

我正在制作一个API,用于保存与另一个模型具有一对多关系的模型。当我在其中应用自动映射时。它给了我以下错误:

    CreateContestDto -> Contest
    tritronAPI.DTOs.CreateContestDto -> tritronAPI.Model.Contest

    Type Map configuration:
    CreateContestDto -> Contest
    tritronAPI.DTOs.CreateContestDto -> tritronAPI.Model.Contest

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

    Mapping types:
    ProblemDto -> Problem
    tritronAPI.DTOs.ProblemDto -> tritronAPI.Model.Problem
    at lambda_method(Closure , ProblemDto , Problem , ResolutionContext )

我的模型是:竞赛和问题竞赛包含许多问题:

    public class Contest
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }
        public string Name { get; set; }
        public DateTime StartTime { get; set; }
        public DateTime EndTime { get; set; }
        public ICollection<Problem> Problems { get; set; }
        public ICollection<ContestProgrammingLanguage> 
        ContestProgrammingLanguages { get; set; }
    }


    public class Problem
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }
        [Required]
        [MaxLength(255)]
        public string ProblemName { get; set; }
        [ForeignKey("User")]
        public string ProblemAuthorId { get; set; }
        public virtual User ProblemAuthor { get; set; }
        public string AuthorName { get; set; }
        //public virtual List<Resources> Resourceses { get; set; }
        public string ProblemDescription { get; set; }
        public bool IsPublished { get; set; }
        public virtual ICollection<Submission> Submissions { get; set; }
        public string Tags { get; set; }
        //public Guid Contest_Id { get; set; }
        public virtual Contest Contest { get; set; }

        [ForeignKey("Contest")]
        public int? Contest_Id { get; set; }
        public short Score { get; set; }

        //Timelimit in miliseconds
        public int TimeLimit { get; set; }

        //MemoryLimit in bytes
        public int MemoryLimit { get; set; }

        //More than source code limit is not allowed
        public int? SourceCodeLimit { get; set; }
        public virtual ICollection<TestFile> TestFiles { get; set; } = new 
        List<TestFile>();
    }


    public class CreateContestDto
    {
        public CreateContestDto()
        {
            this.Problems = new HashSet<ProblemDto>();
        }
        public string Name { get; set; }
        public DateTime StartDate { get; set; }
        public DateTime StartTime { get; set; }
        public DateTime EndDate { get; set; }
        public DateTime EndTime { get; set; }
        public string BackgroundImage { get; set; }
        public string Description { get; set; }
        public ICollection<ProblemDto> Problems { get; set; }
    }


    public class ProblemDto
    {
        public int Id { get; set; }
        public string ProblemName { get; set; }
    }

映射配置文件:

    CreateMap<CreateContestDto, Contest>().ForMember(
    dest => dest.Problems , opt => opt.MapFrom(src => 
    src.Problems));

控制器代码:

    public async Task<IActionResult> AddContest([FromBody] 
    CreateContestDto contest)
    {
        var con = _mapper.Map<Contest>(contest);
        this._uow.ContestRepository.Add(con);
        return Ok();
    }

我已经尝试过使用反向映射在映射配置文件中选择新ID

1 个答案:

答案 0 :(得分:1)

您还需要添加从ProblemDTOProblem的映射:

CreateMap<CreateContestDto, Contest>();

CreateMap<ProblemDto, Problem>();