8

在此处输入图像描述

使用上述 LinqToSql 实体类并尝试映射到同名 DTO 对象会引发 Stackoverflow 异常。我已经阅读过,这似乎是 Child/Parent 与 CriteriaGroup 对象的关系的问题。有没有人有什么建议?

用来调用它的代码:

var mappedDtos = this.configurationRepository
                        .GetAll()
                        .Project()
                        .To<CriteriaGroupDto>();

GetAll() 在哪里:

public override IQueryable<TEntity> GetAll()
{
    return this.Table.AsQueryable();
}    

我为此制作的映射代码是:

/// <summary>
/// The criteria profile.
/// </summary>
public class CriteriaProfile : Profile
{
    /// <summary>
    /// The configure.
    /// </summary>
    protected override void Configure()
    {
        Mapper.CreateMap<CriteriaGroup, CriteriaGroupDto>()
                .ForMember(dest => dest.Groups, opt => opt.MapFrom(src => src.Children))
                .ForMember(dest => dest.Parent, opt => opt.MapFrom(src => src.Parent))
                .ForMember(dest => dest.Criteria, opt => opt.MapFrom(src => src.CriteriaConfigs));

        Mapper.CreateMap<CriteriaConfig, CriteriaConfigDto>()
            .Include<RatingConfig, CriteriaConfigDto>()
            .Include<CountryConfig, CriteriaConfigDto>()
            .Include<TimescaleConfig, CriteriaConfigDto>()
            .ForMember(dest => dest.DefaultValue, opt => opt.UseValue(default(object)))
            .ForMember(dest => dest.DataType, opt => opt.Ignore())
            .ForMember(dest => dest.ElementId, opt => opt.MapFrom(src => src.Id))
            .ForMember(dest => dest.Options, opt => opt.Ignore());

        Mapper.CreateMap<RatingConfig, CriteriaConfigDto>()
            .ForMember(dest => dest.JoinOperators, opt => opt.MapFrom(src => src.JoinOperators))
            .ForMember(dest => dest.OptionOperators, opt => opt.MapFrom(src => src.Operators));

        Mapper.CreateMap<CountryConfig, CriteriaConfigDto>()
            .ForMember(dest => dest.JoinOperators, opt => opt.MapFrom(src => src.JoinOperators))
            .ForMember(dest => dest.OptionOperators, opt => opt.MapFrom(src => src.Operators));

        Mapper.CreateMap<TimescaleConfig, CriteriaConfigDto>()
            .ForMember(dest => dest.JoinOperators, opt => opt.MapFrom(src => src.JoinOperators))
            .ForMember(dest => dest.OptionOperators, opt => opt.MapFrom(src => src.Operators));

        base.Configure();
    }
}

派生类中的附加映射是由于:(EG)

/// <summary>
/// The rating config.
/// </summary>
public partial class RatingConfig
{
    /// <summary>
    /// Gets the operators.
    /// </summary>
    public IEnumerable<RatingOperator> Operators
    {
        get
        {
            return Enumeration.GetAll<RatingOperator>();
        }
    }

    /// <summary>
    /// Gets the join operators.
    /// </summary>
    public IEnumerable<RuleJoinOperator> JoinOperators
    {
        get
        {
            return Enumeration.GetAll<RuleJoinOperator>();
        }
    }
}
4

0 回答 0