本文介绍了Automapper-错误映射类型.映射类型:IEnumerable`1-> IEnumerable`1 System.Collections.Generic.IEnumerable`1 [[TicketTO,app.DTO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个ASP.Net Core C#
应用程序&使用AutoMapper
I have an ASP.Net Core C#
application & using AutoMapper
DTO
public class MovieTO
{
public int Id { get; set;}
public IEnumerable<TicketTO> Tickets { get; set;}
}
public class TicketTO
{
public string Prop1{ get; set;}
public string Prop2{ get; set;}
public string Prop3{ get; set;}
public string Prop4{ get; set;}
}
域实体
public class Movie
{
public int Id { get; set;}
public IEnumerable<BasicTicket> Tickets { get; set;}
}
public class BasicTicket
{
}
public class RegularTicket : BasicTicket
{
public string Prop1{ get; set;}
public string Prop2{ get; set;}
}
public class SpecialTicket : BasicTicket
{
public string Prop3{ get; set;}
public string Prop4{ get; set;}
}
AutoMapper配置
AutoMapper Configuration
public class AppObjectsMapper
{
private readonly IMapper _mapper;
public ObjectsMapper()
{
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<TicketTO, RegularTicket>();
cfg.CreateMap<TicketTO, SpecialTicket>();
cfg.CreateMap<MovieTO, Movie()
});
_mapper = config.CreateMapper();
}
public Movie MapToEntity(MovieTO movie)
{
if(movie.IsSpecial)
{
//#Line1
_mapper.Map<IEnumerable<TicketTO>, IEnumerable<SpecialTicket>>(movie.Tickets);
}
else
{
//#Line2
_mapper.Map<IEnumerable<TicketTO>, IEnumerable<RegularTicket>>(movie.Tickets);
}
return _mapper.Map<MovieTO, Movie>(eventDetail);
}
}
在#line1或#line2处调用映射器时,它将引发以下运行时错误.
When the mapper is called at #line1 or #line2, it throws the below run time error.
如何对此类型进行打字/映射?
How to typecast/map this ?
谢谢!
推荐答案
实际上,您实际上缺少了一些异常明确指出的配置.因此,只需阅读异常并添加缺少的映射即可,例如:
Actually your are missing some of the configuration here which the exception actually clearly says.So just read the exception and add the mapping that is missing like:
cfg.CreateMap<TicketTO, BasicTicket>();
应该可以.最好的祝福罗伯特
That should work.Best regardsRobert
这篇关于Automapper-错误映射类型.映射类型:IEnumerable`1-> IEnumerable`1 System.Collections.Generic.IEnumerable`1 [[TicketTO,app.DTO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!