문제

I'm using the AutoMapper object mapper, but am getting the exception "Custom configuration for members is only supported for top-level individual members on a type."

Basically I have

public class Obj1 
{ 
    public int Id {get;set;} 
} 

and

public class Obj2 
{ 
    public int[] Ids { get; set; } 
} 

Th exception occurs when I try to create the mapping like;

Mapper
    .CreateMap<Obj1, Obj2>()
    .ForMember(d => d.Ids[0], o => o.MapFrom(s => s.Id)
);

Why is this happening ? What I'm wanting to achieve is when the objects are mapping that the single int Id in the source is mapped to the first element in the destination int array e.g [0]. The complete exception is

type="AutoMapper.AutoMapperConfigurationException" message="Custom configuration for members is only supported for top-level individual members on a type." source="AutoMapper"
detail="AutoMapper.AutoMapperConfigurationException: Custom configuration for members is only supported for top-level individual members on a type. at AutoMapper.Impl.ReflectionHelper.FindProperty(LambdaExpression lambdaExpression) at AutoMapper.MappingExpression2.ForMember(Expression1 destinationMember, Action`1 memberOptions) at ...

도움이 되었습니까?

해결책

You are close - just need to make the array instead of setting a individual member

Mapper.CreateMap<Obj1, Obj2>().ForMember(d => d.Ids, o => o.MapFrom(s => new[]{s.Id}));
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top