如何使用Automapper配置MappingProfile?

我正在使用asp.net core 2.2,microsoft.EntityFrameworkCore(2.2.4),microsoft.EntityFrameworkCore.Cosmos(2.2.4),AutoMapper.Extensions.microsoft.DependencyInjection(7.0.0)

这是我的代码:

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;
}

模型

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;
    }
}

在这里,我想知道如何在MappingProfile类中映射图像值? 谁能帮我解决此问题?

qiu397249612 回答:如何使用Automapper配置MappingProfile?

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3059054.html

大家都在问