Hello!
I have two classes:
public class Element
{
public Item Item { get; set; }
}
public class Item
{
public Element Element { get; set; }
}
And I have DTO with same structure for this classes.
This method creates source data for mapping:
static Element[] CreateElements()
{
var element2 = new Element();
return new[]
{
new Element(new ParentItem(element2)),
element2,
new Element()
};
}
Then I configuring mapping and map elements:
Mapper.CreateMap<Element, ElementDto>();
Mapper.CreateMap<Item, ItemDto>();
var elements = CreateElements();
var mappedElements = elements
.Select(_ => Mapper.Map(_, typeof(Element), typeof(ElementDto)))
.OfType<ElementDto>()
.ToArray();
After I check result of mapping:
foreach (var element in mappedElements)
{
Console.WriteLine(mappedElements.Any(e => e?.Item?.Element == element));
}
This code shows "False" three times. It follows that the "element2" from "CreateElements" was created two copies.
As I need to configure the mapping to duplicate is not created? Is it possible?