I'm a little confused. I can not find out the difference between PreserveReferencesand MaxDepth.
Suppose we have the following DTOs and models.
public class PersonEntity
{
public PersonEntity InnerPerson { get; set; }
}
public class PersonModel
{
public PersonModel InnerPerson { get; set; }
}
As stated in the documentation:
Previously, AutoMapper could handle circular references, keeping track of what was mapped, and with each mapping, check the local hashtable of source / destination objects to see if the item was already mapped. It turns out that tracking is very expensive, and you need to choose to use PreserveReferences for circular maps to work. Alternatively, you can customize MaxDepth.
My mappings:
cfg.CreateMap<PersonModel, PersonEntity>().MaxDepth(1);
cfg.CreateMap<PersonEntity, PersonModel>();
Program:
var personModel = new PersonModel();
personModel.InnerPerson = personModel;
var entity = Mapper.Map<PersonEntity>(personModel);
Here is what I expect to get:

Here is what I actually get:

(PreserveReferences MaxDepth) , . MaxDepth? - ? .