Search code examples
c#automapperautomapper-2

How to force Automapper to overwrite array property?


I am using Automapper in my project to map business entities to DTO.

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

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

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

In constructor I am initializing Points property with an empty array to make sure it is always not null. I am using basic configuration for mapping.

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

With Automapper 2.0.0 it worked just fine, but after upgrading to version 2.2.0 I get mapping exception with inner exception:

Index was outside the bounds of the array

It seems Automapper tries to map every member of array, instead of overwriting the whole array. If I remove property initialization from constructor and leave it null, everything works.

Is it possible to configure Automapper 2.2.0 to always overwrite existing array property with new one?


Solution

  • I solved my problem by downgrading to version 2.0.0.