AutoMapper.Mapper does not contain definition for CreateMap

asp.net Mvcasp.net Web-ApiAutomapper

asp.net Mvc Problem Overview


This might be a basic question but wondering I am not getting AutoMapper.Mapper.CreateMap method.

enter image description here

Am I using wrong AutoMapper reference/package? Thanks

asp.net Mvc Solutions


Solution 1 - asp.net Mvc

The static version of the CreateMap method was deprecated in 4.2, then removed from the API in version 5.0. Jimmy Bogard talks about this in more detail in this blog post.

The new technique for mapping is non-static, like this (code is from the post):

var config = new MapperConfiguration(cfg => {
    cfg.CreateMap<Source, Dest>();
});

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

Solution 2 - asp.net Mvc

Here is how I used AutoMapper in my code.

Step 1 : Downloaded AutoMapper through nuget-packages.

Version is

<package id="AutoMapper" version="6.1.1" targetFramework="net452" />

Step 1 : Created DTO class

public class NotificationDTO
{

    public DateTime DateTime { get; private set; }
    public NotificationType Type { get; private set; }
    public DateTime? OriginalDateTime { get; private set; }
    public string OriginalVenue { get; private set; }
    public ConcertDTO Concert { get; set; }
}

public class ConcertDTO
{
    public int Id { get; set; }
    public bool IsCancelled { get; private set; }
    public DateTime DateTime { get; set; }
    public string Venue { get; set; }

}

Step 2 : Created an AutoMapperProfile class which inherits from Profile

using AutoMapper;
using ConcertHub.DTOs;

namespace ConcertHub.Models
{
  public class AutoMapperProfile : Profile
  {
    public AutoMapperProfile()
    {
        CreateMap<Concert, ConcertDTO>();
        CreateMap<Notification, NotificationDTO>();
    }
  }
}

Step 3 : Registered AutoMapperProfile in the Application Start method of Global.asax file

protected void Application_Start()
    {
        AutoMapper.Mapper.Initialize(cfg => cfg.AddProfile<AutoMapperProfile>());
        
    }

Finally the magic piece of code in the Api Controller

public IEnumerable<NotificationDTO> GetNewNotification()
    {
        var userId = User.Identity.GetUserId();
        var notifications = _dbContext.UserNotifications
                .Where(un => un.UserId == userId && !un.IsRead)
                .Select(un => un.Notification)
                .Include(n => n.Concert)
                .ProjectTo<NotificationDTO>()//use Automapper.QueryableExtension namespace
                .ToList();

        return notifications;
    }

Hope it helps .

Solution 3 - asp.net Mvc

Here is how it works now:

        Mapper.Initialize(cfg =>
        {
            cfg.CreateMap<SupervisorEmployee, SupervisorViewModel>()
            .ForMember
                (dst => dst.Name, src => src.MapFrom<string>(e => SupervisorViewModel.MapName(e)))
            .ForMember
                (dst => dst.OfficePhone, src => src.MapFrom<string>(e => e.OfficePhone.FormatPhone(e.OfficePhoneIsForeign)))
            .ForMember
                (dst => dst.HomePhone, src => src.MapFrom<string>(e => e.HomePhone.FormatPhone(e.HomePhoneIsForeign)))
            .ForMember
                (dst => dst.MobilePhone, src => src.MapFrom<string>(e => e.MobilePhone.FormatPhone(e.MobilePhoneIsForeign)));
        });

Solution 4 - asp.net Mvc

I see your class didn't inherit from AutoMapper.Profile

I did this and worked for me

public class AutoMapperConfig: AutoMapper.Profile

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSamiView Question on Stackoverflow
Solution 1 - asp.net MvcWill RayView Answer on Stackoverflow
Solution 2 - asp.net MvcksgView Answer on Stackoverflow
Solution 3 - asp.net MvcMichael KView Answer on Stackoverflow
Solution 4 - asp.net MvcJason ChenView Answer on Stackoverflow