0
votes

I am a newbie in using automapper and I want to implement it in my project. I am trying to map multiple model from EF to single viewmodel in asp project but before doing that I have encountered a problem as below. enter image description here

I tried to follow solution provided as:

  1. Automapper missing type map configuration or unsupported mapping
  2. Automapper missing type map configuration or unsupported mapping?

but without any success. I am using recent automapper. I tried variation of method to create map such as

    config.CreateMap<tblMeeting, MeetingViewModels>()
            .ForMember(dest => dest.meetingDetails, input => input.MapFrom(i => new tblMeeting
            {
                meetingId = i.meetingId,
                meetingType = i.meetingType??null,
                startTime = i.startTime,
                finishTime = i.finishTime,
                meetingDate = i.meetingDate,
                meetingNotes = i.meetingNotes,
                meetingVenue = i.meetingVenue
            }));

and this

            config.CreateMap<tblMeeting, MeetingViewModels>()
           .ForMember(dest => dest.meetingDetails.meetingId, opt => opt.MapFrom(s => s.meetingId))
            .ForMember(dest => dest.meetingDetails.startTime,
                        opt => opt.MapFrom((s => s.startTime)))
            .ForMember(dest => dest.meetingDetails.finishTime,
                        opt => opt.MapFrom(s => s.finishTime))
            .ForMember(dest => dest.meetingDetails.meetingType,
                        opt => opt.MapFrom(s => s.meetingType ?? null))

            .ForMember(dest => dest.meetingDetails.meetingDate,
                        opt => opt.MapFrom(s => s.meetingDate))
            .ForMember(dest => dest.meetingDetails.meetingVenue,
                        opt => opt.MapFrom(s => s.meetingVenue))
            .ForMember(dest => dest.meetingDetails.meetingNotes,
                        opt => opt.MapFrom(s => s.meetingNotes));


        });

this also

             config.CreateMap<tblMeeting, MeetingViewModels>().ConvertUsing<test();

    public class test : ITypeConverter<tblMeeting, MeetingViewModels>
    {


        public MeetingViewModels Convert(tblMeeting source, MeetingViewModels destination, ResolutionContext context)
        {
            MeetingViewModels m = new MeetingViewModels();

            m.meetingDetails.meetingId = Guid.Parse(source.meetingType.ToString());
            m.meetingDetails.meetingNotes = source.meetingNotes;
            m.meetingDetails.meetingType = Guid.Parse(source.meetingType.ToString());
            m.meetingDetails.meetingDate = source.meetingDate;
            m.meetingDetails.startTime = source.startTime;
            m.meetingDetails.finishTime = source.finishTime;
            m.meetingDetails.meetingVenue = source.meetingVenue;

                return m;
        }
    }

but non could solve the problem. if anyone could help me out it would be of great help. Thank you.

1

1 Answers

3
votes

Here is how I personally implement AutoMapper in my projects:

First create a MappingConfig class, generally I put it in App_Code folder.

In my projects I probably have different sections in the system, by section I mean different Areas or somehow the application needs to be logically separated in different parts like User Management, Meetings etc whatever you have there...

So from the moment that I can divide the system in logical sections I create a profile class for each section:

Here is an example of profile class:

public class GeneralMappingConfigProfile : Profile
{ 
    public GeneralMappingConfigProfile()
    {
        CreateMap<sourceObject, destinationObject>()
                .ForMember(d => d.X, o => o.MapFrom(s => s.Y))
    }
}

The class above is an example for general mappings but you may have there a Meetings profile class if it is big enough to be distinguished as a section.

Then in my config class I configure all profile classes as below:

public class MappingConfig
{
    public static void RegisterMappings()
    {
        Mapper.Initialize(config =>
        {
            config.AddProfile<GeneralMappingConfigProfile>(); 
            config.AddProfile<MeetingsMappingConfigProfile>();
            //etc
        });
    }
}

In the global.asax I call the static method like below:

MappingConfig.RegisterMappings();

Then I can create mappings as many as I see fit in each profile:

I just wrote all this code so you can organize the code better...

For your situation might be a lot of things that might cause this error but refer to this question here. Can you please share more code here because would like to see the MeetingViewModels model and the action code because there must be something wrong at the way how you get the tblMeeting object from database.