我收到“缺少类型映射配置或不支持的映射”的一般错误。当我尝试创建此地图时。有什么想法吗?
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 :(得分:0)
此错误的一个症状是您未在应用程序根目录中调用Configure()。如果您的配置存在问题,我建议您进行单元测试并调用Automapper的AssertConfigurationIsValid()
- Configuration Validation Page
[TestMethod]
public void BaseMapperWorks()
{
//MapperConfig is my static MapperCongfiguration Class
MapperConfig.Configure();
Mapper.AssertConfigurationIsValid();
}
AutoMapper Validatior会为您提供映射错误的所有内容