Automapper - Error mapping types. Mapping types: IEnumerable`1 -> IEnumerable`1 System.Collections.Generic.IEnumerable`1[[TicketTO, app.DTO

自古美人都是妖i 提交于 2019-12-25 01:55:24

问题


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: IEnumerable1 -> IEnumerable1 System.Collections.Generic.IEnumerable1[[TicketTO, app.DTO, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] -> System.Collections.Generic.IEnumerable1[[Domain.SpecialTicket, myapp.domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]

How to typecast/map this ?

Thanks!


回答1:


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 regards Robert



来源:https://stackoverflow.com/questions/58382676/automapper-error-mapping-types-mapping-types-ienumerable1-ienumerable1

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!