2

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; }
}
4
  • You're doing it wrong. Check the docs. Commented Jan 24, 2019 at 10:55
  • Yes, that seems to be it. how did i miss that Doc! thank you so much!!! Commented Jan 24, 2019 at 12:07
  • Possible duplicate of How to setup Automapper in ASP.NET Core Commented Jan 25, 2019 at 12:32
  • A complete answer with an example click this link Commented May 25, 2019 at 6:12

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.