AutoMapper.Mapper does not contain definition for CreateMap
Asked Answered
M

4

101

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

Mahler answered 5/7, 2016 at 1:29 Comment(0)
H
143

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);
Hildahildagard answered 5/7, 2016 at 2:18 Comment(6)
Thanks Will, wondering if you can advise how to use .ForMember() method, as couldn't find exact answer for my needs.Mahler
Thank I found the way as below: might be helpful for others var configStack = new MapperConfiguration( cfg => { cfg.CreateMap<StackInfoVM, StackNameVM>().ForMember(dest => dest.stackId, opts => opts.MapFrom(src => src.itemId)) ; } );Mahler
I've same issue, i have tried as you suggested but gives me an error The type or namespace name 'MapperConfiguration' could not be found (are you missing a using directive or an assembly reference?) and also same for IMapper Can you please help me.Zenobia
Could I add all mapping config in one config for all of my model?Anchusin
@MasterProgrammer Yup! I most commonly see the configuration created as a static property with all the mappings created inside of it.Hildahildagard
CreateMap<DomainObject, Dto>();Segmentation
O
50

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 .

Opportune answered 30/7, 2017 at 10:19 Comment(6)
Easier to understand. Thanks.Terat
This looks like Mosh's tutorial from Pluralsight :-)Assist
@Assist you are right dear :-). Glad it helped others.Opportune
Well explained. Thanks.Better to explain in a good Practice way instead of just dumping the lines of code.Warrior
Just wondering if this is working for Auto Mapper Version after Version 5.0?. The issue I am facing is, its working for version 4.1.1. But the moment I change it to a version higher than 4.0, its saying Mapper dont have CreateMap.Battik
There's no Mapper.Initialize method anymore.Lorrin
E
19

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)));
        });
Einberger answered 17/2, 2017 at 20:35 Comment(0)
D
2

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

I did this and worked for me

public class AutoMapperConfig: AutoMapper.Profile
Delindadelineate answered 22/8, 2020 at 4:56 Comment(2)
Why is this not the to answer?Fancie
no idea... guess not that many people make this mistakeDelindadelineate

© 2022 - 2024 — McMap. All rights reserved.