AutoMapper/AutoMapper.Extensions.ExpressionMapping

MapExpression for sub-entity fails

oklipilin opened this issue · 1 comments

https://stackoverflow.com/questions/61928827/automapper-mapexpression-for-sub-entity-fails
Classes:

public class CountyInfo
{
    public CountyInfo()
    {
        Citizens = new HashSet<Citizen>();
    }
    public Guid Id { get; set; }
    public string Name { get; set; }
    public ICollection<Citizen> Citizens { get; set; }
}

public class Citizen
{
    public Guid Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string ZipCode { get; set; }
}

public class CountyInfoDto
{
    public CountyInfoDto()
    {
        Citizens = new List<CitizenDto>();
    }
    public Guid Id { get; set; }
    public string Name { get; set; }
    public List<CitizenDto> Citizens { get; set; }
}

public class CitizenDto
{
    public Guid Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string ZipCode { get; set; }
}

Mappings:

CreateMap<CountyInfo, CountyInfoDto>().ReverseMap();
CreateMap<Citizen, CitizenDto>().ReverseMap();

Use case:

Expression<Func<CountyInfoDto, CountyInfoDto>> selector = c =>
new CountyInfoDto
{
    Id = c.Id,
    Citizens = c.Citizens.Select(p => new CitizenDto
    {
    }).ToList()
};

var resEx = mapper.MapExpression<Expression<Func<CountyInfo, CountyInfoDto>>>(selector);

This mapping fails with error Expression of type DTOs.CitizenDto cannot be used for return type Entities.Citizen however in CountyInfoDto property Citizens has type CitizenDto.
Issue reproduced with v3.0.6 since for test in v.3.1.0 it's blocked with #72.

This like #72 should be fixed in version 3.1.1-preview01. The exception should go away but both parameter and body will be mapped.