Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I get AutoMapper to not cache mapped objects?
    text
    copied!<p>When AutoMapper encounters an object that's already been mapped, it seems to use that object again, instead of trying to re-map it. I believe it does this based on <code>.Equals()</code>.</p> <p>I have a tree that's being mapped. So, a node with some properties, and children. More than one of the nodes have the same value of <code>.Equals()</code>, because it's based off an Id property. The children of the nodes are different and I need those re-mapped, but it's using a cached map value.</p> <p>Is there a way to turn the cached mapping off? All I can think of is implementing a new converter, but that totally defeats the purpose of using AutoMapper.</p> <p>Here is an example on how to reproduce.</p> <pre><code>void Main() { var source = new List&lt;Tag&gt; { new Tag { Id = 1, Name = "Tag 1", ChildTags = new List&lt;Tag&gt; { new Tag { Id = 2, Name = "Tag 2", ChildTags = new List&lt;Tag&gt; { new Tag {Id = 3, Name = "Tag 3"}, new Tag {Id = 4, Name = "Tag 4"} } } } }, new Tag { Id = 1, Name = "Tag 1" }, new Tag { Id = 3, Name = "Tag 3", ChildTags = new List&lt;Tag&gt; { new Tag {Id = 4, Name = "Tag 4"} } } }; Mapper.CreateMap&lt;Tag, Tag&gt;(); var results = Mapper.Map&lt;IList&lt;Tag&gt;, IList&lt;Tag&gt;&gt;(source); results.Dump(); } public class Tag { public int Id { get; set; } public string Name { get; set; } public IEnumerable&lt;Tag&gt; ChildTags { get; set; } public override bool Equals(Object obj) { if (obj == null) { return false; } var x = this; var y = (Tag)obj; return x.Id.Equals(y.Id); } public override int GetHashCode() { return Id.GetHashCode(); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload