0

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

private async Task<List<CountryDTO>> GetCountriesAsync(int dftLanguageId, int localeLangId)
{
    int pageNo = 1, pageSize = 100;
    var countries = new List<CountryDTO>();
    // Get all the Flag images
    var images = await _dbContext.Images.Where(im => im.ImageType.Equals((int)ImageType.FlagPNG) && im.ImageType.Equals((int)ImageType.FlagSVG)).ToListAsync();
    countries = await _dbContext.Countries.Where(cc => cc.IsPublished.Equals(true) && cc.LanguageId.Equals(dftLanguageId)).Select(dfc => new CountryDTO{Uuid = dfc.CountryId, PNGImagePath = images.Where(im => im.ImageId.Equals(dfc.PNGImageId) && im.ImageType.Equals((int)ImageType.FlagPNG)).Select(c => c.FilePath).FirstOrDefault(), SVGImagePath = images.Where(im => im.ImageId.Equals(dfc.SVGImageId) && im.ImageType.Equals((int)ImageType.FlagSVG)).Select(c => c.FilePath).FirstOrDefault(), DisplayName = dfc.DisplayName, DisplayNameShort = dfc.DisplayName, Name = Helper.ReplaceChars(dfc.DisplayName), Path = Helper.ReplaceChars(dfc.DisplayName), CompleteResponse = true}).Skip((pageNo - 1) * 100).Take(pageSize).ToListAsync();
    return countries;
}

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 want to know how to map the image values in the MappingProfile class ? Can anyone help me to fix this issue?

santosh kumar patro
  • 7,231
  • 22
  • 71
  • 143
  • normally you'd write something like CreateMap().HasMany(c => c.Images) if you'd had built up your model correctly. but that wouldn't even be necessary, since EF can figure that out itself. EF then also supprts an include method, if it's not implicit. what does you image class look like? and why don't you just add the image itself to the country model instead of it's id? And why do you use where clauses if you're just going to get the firstordefault? – Glenn van Acker Nov 21 '19 at 11:55
  • Images entity is a shared entity in this case. In short it is used across other entities. Also regarding the where clause , here I am passing the ImageId specific to that country to get the image url details – santosh kumar patro Nov 21 '19 at 13:13
  • Any help with code sample will help me a lot. – santosh kumar patro Nov 21 '19 at 13:14
  • I don't see the images entity in the country entity, only the id. You can add the images entity to the country, and specify in the mapping or with data annotations. look here: https://learn.microsoft.com/en-us/ef/core/modeling/relationships – Glenn van Acker Nov 21 '19 at 13:19
  • also: images.FirstOrDefault(im => im.ImageId.Equals(dfc.PNGImageId) && im.ImageType.Equals((int)ImageType.FlagPNG)).Select(c => c.FilePath); – Glenn van Acker Nov 21 '19 at 13:21
  • Thanks for your response :) – santosh kumar patro Nov 21 '19 at 13:39

0 Answers0