Automapper:“Missing type map configuration or unsupported mapping”
问题描述:
AssertConfigurationIsValid通过,并且正在尝试的对象已完全填充,但在第一个调用的Map请求中出现错误。Automapper:“Missing type map configuration or unsupported mapping”
我试图映射
Survey ToLoad = Mapper.Map<Survey>(U);
我初始化automapper与下面的代码。
//Lots of other Maps
Mapper.Initialize(cfg => cfg.CreateMap<User, SMUser>()
.ForMember(t => t.AccountType, s => s.MapFrom(so => so.AccountType != null ? so.AccountType : String.Empty))
.ForMember(t => t.Username, s => s.MapFrom(so => so.Username != null ? so.Username : String.Empty)));
Mapper.Initialize(cfg => cfg.CreateMap<SurveyMonkey.Containers.Survey, Survey>().ForMember(t => t.AnalyzeUrl, s => s.MapFrom(so => so.AnalyzeUrl != null ? so.AnalyzeUrl : String.Empty))
.ForMember(t => t.Category, s => s.MapFrom(so => so.Category != null ? so.Category : String.Empty))
.ForMember(t => t.CollectUrl, s => s.MapFrom(so => so.CollectUrl != null ? so.CollectUrl : String.Empty))
.ForMember(t => t.EditUrl, s => s.MapFrom(so => so.EditUrl != null ? so.EditUrl : String.Empty))
.ForMember(t => t.Language, s => s.MapFrom(so => so.Language != null ? so.Language : String.Empty))
.ForMember(t => t.Preview, s => s.MapFrom(so => so.Preview != null ? so.Preview : String.Empty))
.ForMember(t => t.SummaryUrl, s => s.MapFrom(so => so.SummaryUrl != null ? so.SummaryUrl : String.Empty))
.ForMember(t => t.Title, s => s.MapFrom(so => so.Title != null ? so.Title : String.Empty))
//Some more members
);
//LISTS
Mapper.Initialize(cfg => cfg.CreateMap<List<SurveyMonkey.Containers.Collector>, List<Collector>>());
//Lots of other List Maps
我正在使用Nuget(5.2.0)的最新稳定版本。
答
只有在整个配置中调用Mapper.Initialize
一次,否则您将覆盖它。
你可以用配置在继承AutoMapper.Profile
类:
using AutoMapper;
public class MyAutoMapperProfile : Profile {
protected override void Configure() {
CreateMap<User, SMUser>();
CreateMap<SurveyMonkey.Containers.Survey, Survey>();
CreateMap<List<SurveyMonkey.Containers.Collector>, List<Collector>>();
}
}
使用该资料然后初始化映射:
Mapper.Initialize(cfg => {
cfg.AddProfile<MyAutoMapperProfile>();
cfg.AddProfile<OtherAutoMapperProfile>();
});
+0
这个工作完美(除了配置保护不公开吗?) –
+0
你是对的,它是受保护的,我编辑了答案。 –
请告诉我的错误,你得到什么? – CodingYoshi