automapperasp.net-core-2.2automapper-8

Configuring AutoMapper in ASP.NET Core


I am trying to use automapper 8.0.0 to fill a list of WorkViewModel. This list is getting data from the Work class out of the database using entity framework.

How ever it looks like something is going wrong with initializing as throwing follows error:

InvalidOperationException: Mapper not initialized

What am I doing wrong?

I have setup the following code!

Startup.cs

services.AddAutoMapper();

Function being called:

public async Task<IEnumerable<WorkViewModel>> GetAllAsync()
{
    IList<Work> works = await _context.Work.ToListAsync();

    IList<WorkViewModel> viewModelList = Mapper.Map<IList<Work>, IList<WorkViewModel>>(works);

    return viewModelList;
}

Configuration:

public class MappingProfile : Profile
{
    public MappingProfile()
    {
        Mapper.Initialize(cfg =>
        {
            cfg.CreateMap<WorkViewModel, Work>();
        });
    }
}

WorkViewModel:

public class WorkViewModel
{
    public int WorkID { get; set; }
    public string Name { get; set; }
    public byte[] Tumbmail { get; set; }
    public string Discription { get; set; }

    public string Client { get; set; }
    public DateTime Date { get; set; }
    public string PreviewLink { get; set; }
    public string GitLink { get; set; }
    public string DownloadLink { get; set; }

    public int DetailID { get; set; }
    public byte[] Banner { get; set; }
    public string Documentation { get; set; }

    public int CategoryID { get; set; }
    public string Category { get; set; }
}

Work Model:

public class Work
{
    [Key]
    public int WorkID { get; set; }

    [Display(Name = "Project Name")]
    public string Name { get; set; }

    [Display(Name = "Client name")]
    public string Client { get; set; }

    [Display(Name = "Description")]
    public string Discription { get; set; }

    [Display(Name = "Date")]
    public DateTime Date { get; set; }

    [Display(Name = "Thumbmail")]
    public byte[] Tumbmail { get; set; }

    [Display(Name = "Preview Link")]
    public string PreviewLink { get; set; }

    [Display(Name = "Git Link")]
    public string GitLink { get; set; }

    [Display(Name = "DownloadLink")]
    public string DownloadLink { get; set; }


    public WorkCategory workCategory { get; set; }
    public WorkDetailed WorkDetailed { get; set; }
}

Solution

  • Only adding services.AddAutoMapper(); to the ConfigureServices method would not work for you. You have to configure AutoMapper as follows:

    public void ConfigureServices(IServiceCollection services)
    {
       // Auto Mapper Configurations
        var mappingConfig = new MapperConfiguration(mc =>
        {
            mc.AddProfile(new MappingProfile());
        });
    
        IMapper mapper = mappingConfig.CreateMapper();
        services.AddSingleton(mapper);
    
        services.AddMvc();
    }
    

    And also don't forget to install the AutoMapper.Extensions.Microsoft.DependencyInjection nuget package.