0

AutoMapper를 사용하고 있습니다. 내 소스 객체에 간단한 클래스에게 있습니다AutoMapper MS Dynamics CRM에서 문자열을 OptionSet 값에 매핑

public class Source 
    { 

     public string FirstName { get; set; } 

     public string type{ get; set; } 
} 

내 목적지가 옵션 이름 유형 설정이 포함 된 MS 역학 CRM 엔티티 (내가 CrmSvctil를 사용하여 모델을 생성 한)입니다. 다음

내 매핑

AutoMapper.Mapper.CreateMap<Source, Destination>() 
       .ForMember(dest => dest.type, opt => opt.MapFrom(src => src.type)); 

입니다 내가 유형 불일치를 오류입니다 얻고있다

기본적으로 내 문제는 내가 사용하여 옵션 설정 값에 문자열을 매핑하는 방법을 모르는

입니다 AutoMapper

답변

0

OptionSets는 Int 유형의 Value 속성이있는 OptionSetValues로 저장됩니다. 문자열, 따라서 형식 불일치 오류가 발생했습니다. 당신의 유형은 실제 INT 인 경우

, 당신은 단지 그것을 구문 분석해야합니다

AutoMapper.Mapper.CreateMap<Source, Destination>() 
      .ForMember(dest => dest.type, opt => opt.MapFrom(src => new OptionSetValue(int.parse(src.type)))); 

를하지만 옵션 세트에 대한 실제 텍스트 값이 있다면, 당신은 OptionSetMetaData를 사용하여 텍스트 값을 조회해야합니다 :

public OptionMetadataCollection GetOptionSetMetadata(IOrganizationService service, string entityLogicalName, string attributeName) 
{ 
    var attributeRequest = new RetrieveAttributeRequest 
    { 
     EntityLogicalName = entityLogicalName, 
     LogicalName = attributeName, 
     RetrieveAsIfPublished = true 
    }; 
    var response = (RetrieveAttributeResponse)service.Execute(attributeRequest); 
    return ((EnumAttributeMetadata)response.AttributeMetadata).OptionSet.Options; 
} 

var data = GetOptionSetMetadata(service, "ENTITYNAME", "ATTRIBUTENAME"); 
AutoMapper.Mapper.CreateMap<Source, Destination>() 
      .ForMember(dest => dest.type, opt => opt.MapFrom(src => new OptionSetValue(optionList.First(o => o.Label.UserLocalizedLabel.Label == src.type))));