AutoMapper。Mapper不包含CreateMap的定义

时间:2021-09-11 16:57:18

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

这可能是一个基本的问题,但我想知道我没有获得automap . mapper。CreateMap方法。

AutoMapper。Mapper不包含CreateMap的定义

Am I using wrong AutoMapper reference/package? Thanks

我使用了错误的自动制图参考/包吗?谢谢

3 个解决方案

#1


76  

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.

CreateMap方法的静态版本在4.2中被弃用,然后在5.0版本中从API中删除。Jimmy Bogard在这篇博文中更详细地谈到了这一点。

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

新的映射技术是非静态的(代码来自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);

#2


20  

Here is how I used AutoMapper in my code.

下面是我如何在代码中使用AutoMapper。

Step 1 : Downloaded AutoMapper through nuget-packages.

步骤1:通过nuget-packages下载AutoMapper。

Version is

版本是

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

Step 1 : Created DTO class

步骤1:创建DTO类

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

步骤2:创建一个从Profile继承的AutoMapperProfile类

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

步骤3:在全局的应用程序启动方法中注册AutoMapperProfile。asax文件

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

    }

Finally the magic piece of code in the Api Controller

最后是Api控制器中的神奇代码

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 .

希望它可以帮助。

#3


14  

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)));
        });

#1


76  

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.

CreateMap方法的静态版本在4.2中被弃用,然后在5.0版本中从API中删除。Jimmy Bogard在这篇博文中更详细地谈到了这一点。

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

新的映射技术是非静态的(代码来自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);

#2


20  

Here is how I used AutoMapper in my code.

下面是我如何在代码中使用AutoMapper。

Step 1 : Downloaded AutoMapper through nuget-packages.

步骤1:通过nuget-packages下载AutoMapper。

Version is

版本是

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

Step 1 : Created DTO class

步骤1:创建DTO类

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

步骤2:创建一个从Profile继承的AutoMapperProfile类

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

步骤3:在全局的应用程序启动方法中注册AutoMapperProfile。asax文件

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

    }

Finally the magic piece of code in the Api Controller

最后是Api控制器中的神奇代码

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 .

希望它可以帮助。

#3


14  

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)));
        });