asp.net-mvc AutoMapper.Mapper 不包含 CreateMap 的定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38194012/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-07 23:33:19  来源:igfitidea点击:

AutoMapper.Mapper does not contain definition for CreateMap

asp.net-mvcasp.net-web-apiautomapper

提问by Sami

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

这可能是一个基本问题,但想知道我没有得到 AutoMapper.Mapper.CreateMap 方法。

enter image description here

在此处输入图片说明

Am I using wrong AutoMapper reference/package? Thanks

我是否使用了错误的 AutoMapper 参考/包?谢谢

回答by Will Ray

The static version of the CreateMapmethod 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):

映射的新技术是非静态的,像这样(代码来自帖子):

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

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

回答by ksg

Here is how I used AutoMapper in my code.

这是我在代码中使用 AutoMapper 的方式。

Step 1 : Downloaded AutoMapperthrough nuget-packages.

第 1 步:通过nuget-packages下载AutoMapper

Version is

版本是

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

Step 1 : Created DTOclass

第 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 AutoMapperProfileclass which inherits from Profile

第 2 步:创建一个继承自ProfileAutoMapperProfile

using AutoMapper;
using ConcertHub.DTOs;

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

Step 3 : Registered AutoMapperProfilein the Application Startmethod of Global.asaxfile

第三步:在Global.asax文件的Application Start方法中注册AutoMapperProfile

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 .

希望能帮助到你 。

回答by Michael K

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