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;}
}
Domain Entity
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 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);
}
}
When the mapper is called at #line1 or #line2, it throws the below run time error.
Error mapping types. Mapping types: IEnumerable
1 -> IEnumerable
1 System.Collections.Generic.IEnumerable1[[TicketTO, app.DTO, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] -> System.Collections.Generic.IEnumerable
1[[Domain.SpecialTicket, myapp.domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]
How to typecast/map this ?
Thanks!
IEnumerable<BasicTicket>
andBasicTicket
? If you think you can shove both types of tickets intoIEnumerable<BasicTicket>
and read them out, it's not possible. This could be related to the issue but only by design. – CodingYoshiMapToEntity
. – CodingYoshiAssertConfigurationIsValid
. And look at the full error message. – Lucian Bargaoanu