2013-06-17 2 views
0

'누락 된 유형 맵 구성 또는 지원되지 않는 매핑'이라는 일반 오류가 발생합니다. 이지도를 만들려고 할 때. 어떤 아이디어?Automapper 클래스에서 Enum으로 매핑

 Mapper.CreateMap<MyEnum, MyClass>().ConvertUsing(c => 
    { 
     MyAttribute attribute = c.GetCustomAttribute<MyEnum, MyAttribute>(); 
     return new MyClass() 
     { 
      Id = c.ToString(), 
      Name = attribute == null ? c.ToString() : attribute.DisplayName 
     }; 
    }); 

그리고 ...

protected override void Configure() 
    { 
    base.Configure(); 

    Mapper.CreateMap<MyEnum, MyClass>() 
     .ForMember(d => d.Id, opt => opt.MapFrom(s => s.ToString())) 
     .ForMember(d => d.Name, opt => opt.ResolveUsing<DisplayNameResolver>()); 
    } 

    private class DisplayNameResolver : ValueResolver<MyEnum, string> 
    { 
    protected override string ResolveCore(MyEnum e) 
    { 
     MyAttribute attribute = e.GetCustomAttribute<MyEnum, MyAttribute>(); 
     return attribute == null ? e.ToString() : attribute.DisplayName; 
    } 
    } 

이 작동하지 마십시오.

감사합니다.

답변

0

이 오류의 한 가지 증상은 응용 프로그램 루트에서 Configure()를 호출하지 않은 것입니다. 구성에 문제가있는 경우 ... 난 당신이 단위 테스트을 제안하고 부를 것이다 Automapper의 AssertConfigurationIsValid() - Configuration Validation Page

[TestMethod] 
public void BaseMapperWorks() 
{ 
    //MapperConfig is my static MapperCongfiguration Class 
    MapperConfig.Configure(); 
    Mapper.AssertConfigurationIsValid(); 
} 

AutoMapper Validatior 당신에게 당신의 매핑 잘못된 모든 것을 줄 것이다