는 Automapper는 System.ArgumentException
나는 ItemToMap
의 목록을 가지고 이러한 모든 개체가 ... 나는 그것의 버전 2에 AutoMapper을 업데이트했고 지금은 그것으로 많은에게 문제가있어 같은 객체 Tag
내가 ItemToMapDto
와 ItemToMap
를 매핑 할에 대한 참조, 나는이 오류를 가지고있어 : 여기
맵핑는 다음과 같습니다AutoMapper.AutoMapperMappingException :
Mapping types: Tag -> TagDto DAL.Entities.Tag -> DTO.Objects.TagDto
Destination path: ItemToMap[][1].Tag.Tag
Source value: Entities.Tag ----> System.ArgumentException : An item with the same key has already been added.
var temp = new List<ItemToMap>();
var tag1 = this.RandomTag;
var length = 10;
for (int i = 0; i < length; i++)
{
temp.Add(new ItemToMap()
{
Tag = tag1,
});
}
var record = temp.ToArray();
var mapped = Mapper.Map<ItemToMap[], ItemToMapDto[]>(record);
내 매핑 작업을 할 수있는 솔루션은 무엇인가 :
Mapper.CreateMap<ItemToMap, ItemToMapDto>();
Mapper.CreateMap<Tag, TagDto>();
여기 내 문제를 강조 단위 테스트는입니까? 문제가 모든 코드에 퍼져 있기 때문에 내가 편집 한
... 글로벌 솔루션을 찾고 있어요 :
문제는 아래의 ctor에서 온다, 나는 ctor에의 코드를 언급하는 경우, 모든
public class ItemToMapDto
{
public ItemToMapDto()
{
/* If I comment the line below, all's fine... But it not the behaviour
* I want, I'd like to have a default value for the property...
*/
this.Tag = new TagDto() { Name = this.RandomText };
}
public string Name
{
get;
set;
}
public TagDto Tag
{
get;
set;
}
}
편집 2 ... 잘 작동 :
Automapper가 이미 설정 리졸버를 재사용 할 수 ResolutionContext
를 캐싱한다. 즉, 매퍼를 반복하고 IsMatch
의 호출에서 true를 반환하는 것을 가져옵니다. 이 ResolutionContext
이 캐쉬되어 있는지 어떤지를 확인하기 위해서, destination property가 벌써 설정되어 있을지 어떨지와 문맥의 해시 코드가 검사됩니다. 대상이 Ctor에 설정되어 있으므로 Automapper는 이것이 캐시되지 않았다고 판단하여 캐시되지 않은 해결 프로그램을 호출합니다. 후자의 해결은 캐시하지만 해시 코드가 이미 캐시 저장소로 사용되는 Dictionary
에 존재하기 때문에 실패
이는 문제가되지 않습니다. 나는 모든 것에 대한지도를 만들었습니다. 내 게시물을 업데이트하여 표시했습니다. –
속성은 문제가되지 않습니다. 'string'과'string.IsNullOrEmpty'는 항상 false입니다. –
문제는 Automapper의'ResolutionContext'에 대한 캐싱 정책 때문입니다. 자세한 내용은 편집 2를 참조하십시오. –