【问题标题】:How to force Automapper to overwrite array property?如何强制 Automapper 覆盖数组属性?
【发布时间】:2012-10-04 17:46:43
【问题描述】:

我在我的项目中使用 Automapper 将业务实体映射到 DTO。

public class TransportStop
{
    public Point[] Points { get; set; }
}

public class TransportStopDto
{
    public PointDto[] Points { get; set; }

    public TransportStopDto()
    {
        Points = new PointDto[0];
    }
}

在构造函数中,我使用空数组初始化 Points 属性,以确保它始终不为空。我正在使用基本配置进行映射。

Mapper.CreateMap<Point, PointDto>();
Mapper.CreateMap<TransportStop, TransportStopDto>();

TransportStop stop = new TransportStop()
{
    Points = new Point[]
    {
        new Point() { X = 1, Y = 1 },
        new Point() { X = 2, Y = 2 }
    }
};

TransportStopDto dto = Mapper.Map<TransportStop, TransportStopDto>(stop);

使用 Automapper 2.0.0 它工作得很好,但是在升级到 2.2.0 版本后,我得到了带有内部异常的映射异常:

索引超出了数组的范围

Automapper 似乎试图映射数组的每个成员,而不是覆盖整个数组。如果我从构造函数中删除属性初始化并将其保留为 null,则一切正常。

是否可以将 Automapper 2.2.0 配置为始终用新的数组属性覆盖现有的数组属性?

【问题讨论】:

    标签: c# automapper automapper-2


    【解决方案1】:

    我通过降级到版本 2.0.0 解决了我的问题。

    【讨论】:

    猜你喜欢
    • 2018-01-03
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 2013-06-04
    • 2017-08-13
    • 1970-01-01
    • 1970-01-01
    • 2021-10-13
    相关资源
    最近更新 更多