【问题标题】:AutoMapper mapping from type to type with nested type使用嵌套类型从类型到类型的 AutoMapper 映射
【发布时间】:2016-11-25 19:39:48
【问题描述】:

我正在使用带有实例 API 的最新版本。

我的来源类型:

public class Source {
  public string ValueX { get; set; }
  public string ValueY { get; set; }
  public string ValueZ { get; set; }
}

我的目的地类型:

public class Destination {

  public Destination () { Inner = new Inner(); }

  public string ValueX { get; set; }

  public Inner Inner { get; set; }

  public class Inner {
    public string ValueY { get; set; }
    public string ValueZ { get; set; }
  }

}

我的配置(在配置文件中完成):

  CreateMap<Source, Destination>()
    .ForMember(dest => dest.Inner, opt => opt.MapFrom(src => src));

但这并没有映射内部对象。当我测试配置时,它会抛出一个无用的 AutoMapperConfigurationException 异常。

使这种映射工作的正确方法是什么?

【问题讨论】:

  • 在此之前,您需要在 Source 和 Inner 之间定义一个映射,例如 CreateMap()

标签: c# automapping automapper-5


【解决方案1】:

在此之前,您需要在 Source 和 Inner 之间定义一个映射,例如:

CreateMap<Source, Inner>();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-11
    • 1970-01-01
    • 1970-01-01
    • 2022-11-14
    • 2014-09-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多