【问题标题】:Automapper expression must resolve to top-level memberAutomapper 表达式必须解析为顶级成员
【发布时间】:2012-07-22 21:33:35
【问题描述】:

我正在使用自动映射器来映射源对象和目标对象。当我映射它们时,我得到以下错误。

表达式必须解析为顶级成员。参数名称:lambdaExpression

我无法解决问题。

我的源和目标对象是:

public partial class Source
{
        private Car[] cars;

        public Car[] Cars
        {
            get { return this.cars; }
            set { this.cars = value; }
        }
}

public partial class Destination
{
        private OutputData output;

        public OutputData Output
        {            
            get {  return this.output; }
            set {  this.output= value; }
        }
}

public class OutputData
{
        private List<Cars> cars;

        public Car[] Cars
        {
            get { return this.cars; }
            set { this.cars = value; }
        }
}

我必须将 Source.Cars 映射到 Destination.OutputData.Cars 对象。你能帮我解决这个问题吗?

【问题讨论】:

  • 可以添加自动映射器配置吗?
  • 我尝试添加配置,运行配置时抛出错误。我的配置如下 Mapper.CreateMap().ForMember(dest => dest.OutputData.Cars, input => input.MapFrom(i => i.Cars));表达式 'dest => dest.OutputData.Cars' 必须解析为顶级成员。参数名称:lambdaExpression
  • 这对我有用:Mapper.CreateMap&lt;Destination, Source&gt;().ForMember(x =&gt; x.Cars, x =&gt; x.MapFrom(y =&gt; y.OutputData.Cars)).ReverseMap();(也发布在下面的答案中)

标签: automapper .net automapper-2


【解决方案1】:

您正在使用:

 Mapper.CreateMap<Source, Destination>()
 .ForMember( dest => dest.OutputData.Cars, 
             input => input.MapFrom(i => i.Cars)); 

这不起作用,因为您在 dest lambda 中使用了 2 级。

使用 Automapper,您只能映射到 1 个级别。要解决问题,您需要使用单个级别:

 Mapper.CreateMap<Source, Destination>()
 .ForMember( dest => dest.OutputData, 
             input => input.MapFrom(i => new OutputData{Cars=i.Cars})); 

这样,您可以将您的汽车设置到目的地。

【讨论】:

  • 还使用此解决方案在源具有string somethingKey 和string somethingValue 且目标具有object something { string Key, string Value } 的对象之间进行映射
  • 很好的答案!!
【解决方案2】:
  1. 定义Source 和OutputData 之间的映射。

    Mapper.CreateMap<Source, OutputData>();
    
  2. 更新您的配置以将Destination.Output 与OutputData 映射。

    Mapper.CreateMap<Source, Destination>().ForMember( dest => dest.Output, input => 
        input.MapFrom(s=>Mapper.Map<Source, OutputData>(s))); 
    

【讨论】:

  • 我在 OutputData 中有一些默认值的字段。我在源中没有此字段的映射。所以我没有映射它。映射完成后,我的默认值在目的地设置为空值。如何避免它设置为空值。
【解决方案3】:

你可以这样做:

// First: create mapping for the subtypes
Mapper.CreateMap<Source, OutputData>();

// Then: create the main mapping
Mapper.CreateMap<Source, Destination>().
    // chose the destination-property and map the source itself
    ForMember(dest => dest.Output, x => x.MapFrom(src => src)); 

这就是我的做法;-)

【讨论】:

  • 这是最好的解决方案
  • 当您想要将 IDataRecord 映射到复杂的 DTO 时,效果非常好。
  • 一种更优雅的方式,与投票最多的答案相比,这可能会导致一些非常复杂的语法。
【解决方案4】:

这对我有用:

Mapper.CreateMap<Destination, Source>()
    .ForMember(x => x.Cars, x => x.MapFrom(y => y.OutputData.Cars))
    .ReverseMap();

【讨论】:

    【解决方案5】:

    allrameest 在这个问题上给出的正确答案应该会有所帮助:AutoMapper - Deep level mapping

    这是你需要的:

    Mapper.CreateMap<Source, Destination>()
        .ForMember(dest => dest.OutputData, opt => opt.MapFrom(i => i));
    Mapper.CreateMap<Source, OutputData>()
        .ForMember(dest => dest.Cars, opt => opt.MapFrom(i => i.Cars));
    

    使用映射器时,使用:

    var destinationObj = Mapper.Map<Source, Destination>(sourceObj)
    

    其中destinationObj 是Destination 的一个实例,sourceObj 是Source 的一个实例。

    注意:此时您应该尝试停止使用 Mapper.CreateMap,它已过时并且很快将不受支持。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-06
      • 2023-04-10
      相关资源
      最近更新 更多