AutoMapper ProjectTo() configuration question

asp.net-core automapper c# entity-framework-core

Question

Not able to map one of the Dto's:

ViewModel:

public class TicketViewModel
{
    public TicketDto Ticket { get; set; }

    public CompanyDto Company { get; set; }

    public TicketStateDto TicketState { get; set; }

    public string TicketPriorityName { get; set; }
}

Company and TicketState are navigation properties.

Query:

var query = _ticketRepository.GetAll() // return IQueryable<Ticket>
             .Include(c=> c.Company)
             .Include(tt => tt.TicketState)
             .Include(ts => ts.TicketPriority)
             .OrderBy(n => n.Id)
        .ProjectTo<TicketViewModel>();

Mappings:

configuration.CreateMap<Ticket, TicketDto>();
configuration.CreateMap<TicketState, TicketStateDto >();
configuration.CreateMap<Company, CompanyDto >();

configuration.CreateMap<Ticket, TicketViewModel>()
            .ForMember(dest => dest.Company, conf => conf.MapFrom(src => src.Company ))
            .ForMember(dest => dest.TicketState, conf => conf.MapFrom(src => src.TicketState))
            .ForMember(dest => dest.TicketPriorityName, conf => conf.MapFrom(src => src.TicketPriority.Name)
        );

Ticket data populated in query, but not projected to TicketDto:

enter image description here

How to correctly configure mapping for TicketViewModel?

1
0
9/4/2018 1:46:40 PM

Accepted Answer

Thanks to #5984640, i figured it out:

configuration.CreateMap<Ticket, TicketDto>();
configuration.CreateMap<TicketState, TicketStateDto >();
configuration.CreateMap<Company, CompanyDto >();

configuration.CreateMap<Ticket, TicketViewModel>()
            // .ForMember(dest => dest.Company, conf => conf.MapFrom(src => src.Company ))
            // .ForMember(dest => dest.TicketState, conf => conf.MapFrom(src => src.TicketState))
            // .ForMember(dest => dest.TicketPriorityName, conf => conf.MapFrom(src => src.TicketPriority.Name)
            .ForMember(dest => dest.Ticket, conf => conf.MapFrom(src => src) // <= required configuration
        );

The commented lines Automapper got automatically from the first mappings, so they are unnecessary.

1
9/4/2018 1:43:46 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow