AutoMapper.Mapper.CreateMap<TSource,TDestination>() is obsolete. The static API will be removed in version 5.0. Use a MapperConfiguration instance and store statically as needed. Use CreateMapper to create a mapper instance.

4,585 views
Skip to first unread message

twig...@gmail.com

unread,
Feb 4, 2016, 10:03:24 AM2/4/16
to AutoMapper-users
Hi, I am just getting started with Automapper. I am using version 4.2. I have followed some example that were on the web to get the mapping initialize but seeing the following 

AutoMapper.Mapper.CreateMap<TSource,TDestination>() is obsolete. The static API will be removed in version 5.0. Use a MapperConfiguration instance and store statically as needed. Use CreateMapper to create a mapper instance. What do I need to do get this message to disappear?

I am using the automapper on a web api project.

Here is what I have on the 

 public static class WebApiConfig
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="config"></param>
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services
            // Configure Web API to use only bearer token authentication.
            config.SuppressDefaultHostAuthentication();
            config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));


            var cors = new EnableCorsAttribute("*", "*", "GET");
            config.EnableCors();

            
            // Web API routes
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

           
            AutoMapperConfig.RegisterMappings();
        }
    }


public static void RegisterMappings()
        {

             
            AutoMapper.Mapper.CreateMap<Xrm.api_event, Event>();
           

        }

Jimmy Bogard

unread,
Feb 4, 2016, 10:17:46 AM2/4/16
to automapper-users

--
You received this message because you are subscribed to the Google Groups "AutoMapper-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to automapper-use...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Simba Twiga

unread,
Feb 4, 2016, 10:25:02 AM2/4/16
to automapp...@googlegroups.com
where do I need to put this on the central place on the web.api project. Or do I have to call this every time I call mapper.Map? And what is AppProfile. 
var config = new MapperConfiguration(cfg => {
    cfg.AddProfile<AppProfile>();
    cfg.CreateMap<Source, Dest>();
});

var mapper = config.CreateMapper();
var dest = mapper.Map<Source, Dest>(new Source());

--
You received this message because you are subscribed to a topic in the Google Groups "AutoMapper-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/automapper-users/svx0p4n6MSo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to automapper-use...@googlegroups.com.

Jimmy Bogard

unread,
Feb 4, 2016, 10:37:04 AM2/4/16
to automapper-users
Put it in where the other central Web API config is. Profiles are a way to gather CreateMap calls into a single spot (or multiple).

Basically, you only need to call CreateMap once per AppDomain, the non-static API enforces this.

Simba Twiga

unread,
Feb 4, 2016, 10:44:22 AM2/4/16
to automapp...@googlegroups.com
Yes. Sorry for my ignorance that I am not understanding. Is it possible to see a sample automapper configured on Web API project.

Jimmy Bogard

unread,
Feb 4, 2016, 10:55:23 AM2/4/16
to automapper-users
It's not much different than you have. Instead of calling static methods, call instance ones. Instead of calling Mapper.Map, you can inject an IMapper or create your own static field.

velku...@gmail.com

unread,
Mar 25, 2016, 4:19:04 AM3/25/16
to AutoMapper-users
Hi Jimmy,

I'm using AutoMapper 3.2.1 where i have a single class where i used to Initialize all my mappings as below 

  Mapper.Initialize(cfg =>
            {
                cfg.CreateMap<UserLoginViewModel, User>();
                cfg.CreateMap<LoginViewModel, User>();
            });
Usage

 var user = Mapper.Map<LoginViewModel, User>(model);

I'm trying to upgrade with 4.2.1 i'm facing a problem, like below 

my configuration for 4.2.1
 public static class AutoMapperConfig
    {
        public static void Setup()
        {

            var config = new MapperConfiguration(cfg =>
            {
                cfg.CreateMap<LoginViewModel, User>();
                cfg.CreateMap<UserLoginViewModel, User>();

            });

            config.AssertConfigurationIsValid();
        }
    }

Missing type map configuration or unsupported mapping.

Mapping types:
LoginViewModel -> User
MFA.ViewModel.LoginViewModel -> MFA.Domain.User

Destination path:
User

Source value:
MFA.ViewModel.LoginViewModel

eugen.she...@gmail.com

unread,
Aug 25, 2016, 1:39:26 PM8/25/16
to AutoMapper-users, velku...@gmail.com
Hi, did you fix your problem?
After upgrade automapper I have exactly the same issue

Jimmy Bogard

unread,
Aug 25, 2016, 6:13:02 PM8/25/16
to automapp...@googlegroups.com
Your code can be used exactly the same in 4.2.1, Mapper.Initialize and Mapper.Map.
To unsubscribe from this group and stop receiving emails from it, send an email to automapper-users+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages