简体   繁体   English

Web Api in.Net Core中如何使用AutoMapper?

[英]How to use AutoMapper in Web Api in .Net Core?

Currently, I have this Web Api method which manually map my entity to DTO object and it works without issue.目前,我有这个 Web Api 方法手动将我的实体 map 我的实体到 DTO ZA8CFDE6311.C49EB2AC96F 没有问题Now, I would like to implement AutoMapper which I have installed in registered in in ConfigureServices method and created the AutoMapper profile class and injected automapper in my Web Api controller constructor. Now, I would like to implement AutoMapper which I have installed in registered in in ConfigureServices method and created the AutoMapper profile class and injected automapper in my Web Api controller constructor.

This is my manual mapping without AutoMapper这是我没有 AutoMapper 的手动映射

[HttpGet]
    public async Task<ActionResult<IEnumerable<MovieDto>>> GetMovies()
    {
        //manual mapping which can be replaced with Automapper
        var movies = (from m in _context.Movies
                      select new MovieDto()
                      {
                          Id = m.Id,
                          MovieTitle = m.MovieTitle,
                          ReleaseDate = m.ReleaseDate,
                          MovieStatus = m.MovieStatus,
                          PhotoFile = m.PhotoFile
                      }).ToListAsync();

        return await movies;

    }

and this is how I tried to add automapper to replace manual mapping.这就是我尝试添加自动映射器以替换手动映射的方式。

[HttpGet]
public async Task<ActionResult<IEnumerable<MovieDto>>> GetMovies()
{
      return _mapper.Map<IEnumerable<MovieDto>>(_context.Movies);
}

But, it hits error Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<CinemaApp.NETCore31.WebAPI.Models.MovieDto>' to 'Microsoft.AspNetCore.Mvc.ActionResult<System.Collections.Generic.IEnumerable<CinemaApp.NETCore31.WebAPI.Models.MovieDto>>'但是,它遇到错误Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<CinemaApp.NETCore31.WebAPI.Models.MovieDto>' to 'Microsoft.AspNetCore.Mvc.ActionResult<System.Collections.Generic.IEnumerable<CinemaApp.NETCore31.WebAPI.Models.MovieDto>>'

You will need to create a Profile and map all of the conversions you want to do.您将需要创建一个配置文件和 map 所有您想做的转换。 You don't have to do this field by field, you can just let AutoMapper figure it out as long as the names are the same.您不必逐个字段进行此操作,只要名称相同,就可以让 AutoMapper 弄清楚。

This seems to be a good tutorial: https://code-maze.com/automapper-net-core/这似乎是一个很好的教程: https://code-maze.com/automapper-net-core/

First create a class that inherits from the Profile class in AutoMapper首先在 AutoMapper 中创建一个继承自 Profile class 的 class

public class MappingProfile : Profile 
{
   public MappingProfile()
   {
      CreateMap<Movie, MovieDto>();
   }
}

Note that if you ever want to map from MovieDto to Movie , you will have to create a second CreateMap line.请注意,如果您想从MovieDtoMovie的 map ,则必须创建第二条CreateMap线。

In your Startup.cs class, you need to register Automapper, and this will automatically scan for all profiles in the assembly that the Startup class is defined in with this code:在您的Startup.cs class 中,您需要注册 Automapper,这将自动扫描程序集中使用此代码定义 Startup class 的所有配置文件:

public void ConfigureServices(IServiceCollection services)
{
   services.AddAutoMapper(typeof(Startup));
}

If you are using AutoMapper V9 you can use ProjectTo extensions method like this如果您使用的是AutoMapper V9 ,您可以像这样使用ProjectTo扩展方法

private readonly IMapper _mapper;

public MovieController(IMapper mapper)
{
    _mapper = mapper;
}

[HttpGet]
public async Task<ActionResult<IEnumerable<MovieDto>>> GetMovies()
{
      return await _context.Movies
                .ProjectTo<MovieDto>(_mapper.ConfigurationProvider)
                .ToListAsync();
}

For using ProjectTo you need add AutoMapper.QueryableExtensions namespace.要使用ProjectTo ,您需要添加AutoMapper.QueryableExtensions命名空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM