Automapper ProjectTo not working when used with custom mapping

1.5k views Asked by At

I am using asp.net core 2.2, Microsoft.EntityFrameworkCore(2.2.4), Microsoft.EntityFrameworkCore.Cosmos(2.2.4), AutoMapper.Extensions.Microsoft.DependencyInjection(7.0.0)

Here goes my code:

MappingProfile.cs

public class MappingProfile : Profile
{
    public MappingProfile()
    {
        CreateMap<Languages, LanguageDTO>();        
        CreateMap<Countries, CountryDTO>().ForMember(dest => dest.Uuid, opt => opt.MapFrom(src => src.CountryId)).ForMember(dest => dest.DisplayName, opt => opt.MapFrom(src => src.DisplayName)).ForMember(dest => dest.DisplayNameShort, opt => opt.MapFrom(src => Helper.ReplaceChars(src.DisplayName))).ForMember(dest => dest.Path, opt => opt.MapFrom(src => Helper.ReplaceChars(src.DisplayName)));
    }
}

ServiceClass.cs

countries = await _mapper.ProjectTo<CountryDTO>(_dbContext.Countries.Where(cc => cc.IsPublished.Equals(true) && cc.LanguageId.Equals(dftLanguageId))).ToListAsync();

Models

public class Countries
{
    public string id
    {
        get;
        set;
    }

    public int CountryId
    {
        get;
        set;
    }

    public int? SVGImageId
    {
        get;
        set;
    }

    public int? PNGImageId
    {
        get;
        set;
    }

    public bool IsPublished
    {
        get;
        set;
    }

    public string CreatedBy
    {
        get;
        set;
    }

    public string CreatedDate
    {
        get;
        set;
    }

    public string UpdatedBy
    {
        get;
        set;
    }

    public string UpdatedDate
    {
        get;
        set;
    }

    public int? CountryContentId
    {
        get;
        set;
    }

    public int? LanguageId
    {
        get;
        set;
    }

    public string DisplayName
    {
        get;
        set;
    }

    public string DisplayNameShort
    {
        get;
        set;
    }
}

public class CountryResult
{
    public CountryResult()
    {
        Countries = new List<CountryDTO>();
    }

    public List<CountryDTO> Countries
    {
        get;
        set;
    }
}

public class CountryDTO
{
    [JsonProperty("pngimagePath")]
    [RegularExpression(Constants.GeneralStringRegularExpression)]
    public string PNGImagePath
    {
        get;
        set;
    }

    = "";
    [JsonProperty("svgimagePath")]
    [RegularExpression(Constants.GeneralStringRegularExpression)]
    public string SVGImagePath
    {
        get;
        set;
    }

    = "";
    [RegularExpression(Constants.GeneralStringRegularExpression)]
    public string DisplayName
    {
        get;
        set;
    }

    = "";
    [RegularExpression(Constants.GeneralStringRegularExpression)]
    public string DisplayNameShort
    {
        get;
        set;
    }

    = "";
    [RegularExpression(Constants.GeneralStringRegularExpression)]
    public string ProviderName
    {
        get;
        set;
    }

    = "";
    [RegularExpression(Constants.GeneralStringRegularExpression)]
    public string ProviderTerms
    {
        get;
        set;
    }

    = "";
    public int Uuid
    {
        get;
        set;
    }

    [RegularExpression(Constants.GeneralStringRegularExpression)]
    public string Name
    {
        get;
        set;
    }

    = "";
    [RegularExpression(Constants.GeneralStringRegularExpression)]
    public string Path
    {
        get;
        set;
    }

    = "";
    public bool CompleteResponse
    {
        get;
        set;
    }
}

Here I am seeing that all values are not getting mapped. For example Name is set to empty string and CompleteResponse set to false.

Can anyone help me to fix this issue?

1

There are 1 answers

1
Xueli Chen On BEST ANSWER

Try to use ProjectTo as follows :

var countries = _dbContext.Countries
            .Where(cc => cc.IsPublished.Equals(true) && cc.LanguageId.Equals(dftLanguageId))
            .ProjectTo<CountryDTO>(_mapper.ConfigurationProvider).ToList();

Note : Unless the name of properties between the source and the destination are same, you need to use ForMember() to map the properties one-to-one. Otherwise the value are not getting mapped.