Automapper Project与自定义映射一起使用时不起作用

时间:2019-10-31 14:01:08

标签: c# asp.net-core-2.2 ef-core-2.2 automapper-7

我正在使用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

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

ServiceClass.cs

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

模型

#include "q_learner/q_learner_node.h"

int main(int argc, char * argv[])
{
    rclcpp::init(argc, argv);
    rclcpp::executors::MultiThreadedExecutor exec;
    auto q_learner_node = std::make_shared<q_learner::QLearnerNode>();`//This is the object that never gets destructed.
    exec.add_node(q_learner_node);
    exec.spin();
    rclcpp::shutdown();
    return 0;
}

在这里,我看到所有值都没有被映射。例如,名称设置为空字符串,而CompleteResponse设置为false。

有人可以帮助我解决此问题吗?

1 个答案:

答案 0 :(得分:1)

尝试如下使用ProjectTo

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

注意:除非源和目标之间的属性名称相同,否则需要使用ForMember()一对一映射属性。否则,该值将不会被映射。