Automapper - flattening of object property

Viewed 91

let's say I have

public class EFObject
{
    public int Id { get; set; }
    public int NavId { get; set; }
    public NavObject Nav { get; set; }
}

public class DTOObject
{
    public int Id { get; set; }
    public int NavId { get; set; }
    public string NavName { get; set; }
}

My expectation was high, and I thought to my self the built-in flattening should handle this, so my mapping is very simple

CreateMap<DTOObject, EFObject>().ReverseMap();

Unfortunately, converting DTOObject to EFObject does not work as expected because EFObject.Nav is null. Since I used the name NavId and NavName I would expect it to create a new NavObject and set the Nav.Id and Nav.Name accordingly.

My Problem : Is there a feature in Automapper that will allow me to achieve the intended result without having to manually write a rule to create an NavObject when mapping the Nav property?.

1 Answers

Unflattening is only configured for ReverseMap. If you want unflattening, you must configure Entity -> Dto then call ReverseMap to create an unflattening type map configuration from the Dto -> Entity.

as noted by Automapper documentation here

Related