Asp.net core 缺少DTO集合的类型映射配置或不支持的映射
我正在制作一个API,用于保存一个与另一个模型有一对多关系的模型。当我在其中应用自动映射时。它给了我以下错误:Asp.net core 缺少DTO集合的类型映射配置或不支持的映射,asp.net-core,asp.net-web-api,automapper,Asp.net Core,Asp.net Web Api,Automapper,我正在制作一个API,用于保存一个与另一个模型有一对多关系的模型。当我在其中应用自动映射时。它给了我以下错误: CreateContestDto -> Contest tritronAPI.DTOs.CreateContestDto -> tritronAPI.Model.Contest Type Map configuration: CreateContestDto -> Contest tritronAPI.DTOs.CreateC
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; }
}
公开课竞赛
{
[关键]
[数据库生成(DatabaseGeneratedOption.Identity)]
公共int Id{get;set;}
公共字符串名称{get;set;}
公共日期时间开始时间{get;set;}
公共日期时间结束时间{get;set;}
公共ICollection问题{get;set;}
公共ICollection
程序设计语言{get;set;}
}
公共阶级问题
{
[关键]
[数据库生成(DatabaseGeneratedOption.Identity)]
公共int Id{get;set;}
[必需]
[最大长度(255)]
公共字符串问题名称{get;set;}
[外键(“用户”)]
公共字符串问题Authorid{get;set;}
公共虚拟用户问题作者{get;set;}
公共字符串AuthorName{get;set;}
//公共虚拟列表资源{get;set;}
公共字符串ProblemDescription{get;set;}
公共bool已发布{get;set;}
公共虚拟ICollection提交{get;set;}
公共字符串标记{get;set;}
//公共Guid竞赛\u Id{get;set;}
公共虚拟竞赛{get;set;}
[外键(“竞赛”)]
公共int?竞赛_Id{get;set;}
公共短分数{get;set;}
//时间限制(毫秒)
public int TimeLimit{get;set;}
//内存限制(字节)
public int MemoryLimit{get;set;}
//不允许超过源代码限制
public int?SourceCodeLimit{get;set;}
公共虚拟ICollection测试文件{get;set;}=new
List();
}
公共类CreateConTestedTo
{
public createchalledto()
{
this.Problems=newhashset();
}
公共字符串名称{get;set;}
公共日期时间起始日期{get;set;}
公共日期时间开始时间{get;set;}
公共日期时间结束日期{get;set;}
公共日期时间结束时间{get;set;}
公共字符串背景图像{get;set;}
公共字符串说明{get;set;}
公共ICollection问题{get;set;}
}
公共类问题
{
公共int Id{get;set;}
公共字符串问题名称{get;set;}
}
映射配置文件:
CreateMap<CreateContestDto, Contest>().ForMember(
dest => dest.Problems , opt => opt.MapFrom(src =>
src.Problems));
CreateMap().ForMember(
dest=>dest.Problems,opt=>opt.MapFrom(src=>
src.问题);
控制器代码:
public async Task<IActionResult> AddContest([FromBody]
CreateContestDto contest)
{
var con = _mapper.Map<Contest>(contest);
this._uow.ContestRepository.Add(con);
return Ok();
}
public async Task AddContest([FromBody]
创建竞赛(用于竞赛)
{
var con=_mapper.Map(竞赛);
此._uow.contractRepository.Add(con);
返回Ok();
}
我已经尝试过使用reversemap在映射配置文件中选择新id您还需要为
ProblemDTO
添加映射到Problem
:
CreateMap<CreateContestDto, Contest>();
CreateMap<ProblemDto, Problem>();
CreateMap();
CreateMap();
不需要MapFrom。