4

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).

1
  • Whats the error you get? Commented Dec 1, 2016 at 11:31

2 Answers 2

7

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>();
});

AutoMapper Configuration

Sign up to request clarification or add additional context in comments.

2 Comments

This worked perfectly.(except the configure is protected not public?)
You are right, it is protected, I edited the answer.
3

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.