How do I get AutoMapper to not cache mapped objects?

前端 未结 5 1863
不思量自难忘°
不思量自难忘° 2021-02-04 00:37

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 .Equal

5条回答
  •  情深已故
    2021-02-04 00:42

    It seems that the Equals behaviour of the tree object you're mapping is inappropriate.

    The method should only return true if "the specified object is equal to the current object." - http://msdn.microsoft.com/en-us/library/bsc2ak47.aspx

    In your case, you have two tree objects sharing the same id but clearly, they are not "equal" since they have different children.

    I suggest looking at why the Equals method has been abused in this way and whether you could get the behaviour you need by not overriding the Equals method, instead using a different method to check the tree id field with a more appropriate name eg. TreeIdsAreEqual.

提交回复
热议问题