缺少类型的地图配置或不支持的映射.收集DTO



i正在制作一个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();
    }

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

您还需要将ProblemDTO的映射添加到Problem

CreateMap<CreateContestDto, Contest>();
CreateMap<ProblemDto, Problem>();

相关内容

最新更新