Reputation: 104
AssertConfigurationIsValid Passes, and the object being tried is fully populated, but I get the error on the first Map request called.
I'm trying to map
Survey ToLoad = Mapper.Map<Survey>(U);
I'm initializing automapper with the code below.
//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
I'm using the latest Stable version from Nuget (5.2.0).
Upvotes: 4
Views: 15157
Reputation: 703
I got same error and in my startup class in ConfigureServices methode I use
services.AddAutoMapper(typeof(startup));
=> startup class.
Because of that my automapper profile class (the class that inherit from Automapper.Propfle
, in this case
public class MyAutoMapperProfile : Profile
) not getting read.
To fix this I have replace startup
class with the MyAutoMapperProfile
like below
services.AddAutoMapper(typeof(MyAutoMapperProfile));
.
you can debug and check whether your automapper mapping class getting hit or not.
Upvotes: 3
Reputation: 9463
Only call Mapper.Initialize
once with the whole configuration, or you will overwrite it.
You can wrap the configuration in a class that inherits 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>>();
}
}
Then initialize the Mapper using this Profile:
Mapper.Initialize(cfg => {
cfg.AddProfile<MyAutoMapperProfile>();
cfg.AddProfile<OtherAutoMapperProfile>();
});
Upvotes: 7