Amr Ramadan
Amr Ramadan

Reputation: 1269

Web api with Automapper gives Missing type map configuration or unsupported mapping

I'm Using Web api and I am trying to map the Model to the View Model and vice versa. but I am getting an error message that states that

Missing type map configuration or unsupported mapping

I have the following layers:

and here is my code:

Initialize Class:

 public class DomainToViewModelMap : AutoMapper.Profile
    {
        public override string ProfileName
        {
            get { return "ViewModelToDomainMappings"; }
        }
        protected override void Configure()
        {
            Mapper.Initialize(cfg =>
            {
                cfg.CreateMap<Notification, NotificationModel>();
            });
        }
    }
    public class ViewModelToDomainMap : AutoMapper.Profile
    {
        public override string ProfileName
        {
            get { return "ViewModelToDomainMappings"; }
        }
        protected override void Configure()
        {
            Mapper.Initialize(cfg =>
            {
                cfg.CreateMap<NotificationModel, Notification>();
            });
        }
    }
    public class AutoMapperConfigurationTest
    {
        public static void Configure()
        {
            Mapper.Initialize(x =>
            {
                x.AddProfile<DomainToViewModelMap>();
                x.AddProfile<ViewModelToDomainMap>();
            });
        }
    }

And I am calling it from Application_Start in Global.asax:

    protected void Application_Start()
    {
        AutoMapperConfigurationTest.Configure();
    }

DAL Layer:

    public static NotificationModel GetByID(int ID)
    {
            using (Data.ZajelEntities db = new Data.ZajelEntities())
            {
                var notification = db.Notifications.Find(ID);
                if (notification != null)
                {
                    return Mapper.Map<Notification, NotificationModel>(notification);
                }
            }

        return null;
    }

I know that this issue related to initialization but i am sure the initialization code run through Global.asax of web api

What is missing ?

Upvotes: 0

Views: 452

Answers (1)

Jimmy Bogard
Jimmy Bogard

Reputation: 26765

You need to call the base CreateMap method in your Profile, not Mapper.Initialize. Mapper.Initialize should be called once per AppDomain.

Upvotes: 2

Related Questions