1

I am trying to map my domain model to viewModels in my MVC MVVM application. Here is the code of my Auto Mapper Configuration

public class AutoMapperConfiguration
{
    public static void Configure()
    {
        Mapper.Initialize(x =>
        {
            x.AddProfile<BookStoreMappingProfile>();
        });
    }
}

Mapping Profile:

public class BookStoreMappingProfile: Profile
{
    public static MapperConfiguration InitializeAutoMapper()
    {
        MapperConfiguration config = new MapperConfiguration(cfg =>
        {
            cfg.CreateMap<Book, BookListViewModel>();
        });

        return config;
    }
}

Global.asax

    //AutoMapper Configuration
    AutoMapperConfiguration.Configure();

And my controller code is:

    public ActionResult Index()
    {
        IEnumerable<Book> Books = _bookService.GetAllBooks().ToList();

        IEnumerable<BookListViewModel> BookListViewModel = Mapper.Map<IEnumerable<Book>, IEnumerable<BookListViewModel>>(Books);
        return View(BookListViewModel);
    }

BookListViewModel.cs

public class BookListViewModel
{
    [DisplayName("Name")]
    public string Name { get; set; }
    [DisplayName("ISBN")]
    public string ISBN { get; set; }
    [DisplayName("Price")]
    public double Price { get; set; }
    [DisplayName("Rating")]
    public double Rating { get; set; }
    [DisplayName("Publisher")]
    public string Publisher { get; set; }
}

Book.cs

public class Book
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string ISBN { get; set; }
    public double Price { get; set; }
    public double Rating { get; set; }
    public string Publisher { get; set; }
    public DateTime YearPublished { get; set; }
    public string Edition { get; set; }
    public int CategoryId { get; set; }
    public virtual Category Categories { get; set;}
}

When I try to get my Index method in the controller, this exception is thrown:

Attempt by method 'AutoMapper.MapperConfiguration.FindClosedGenericTypeMapFor(AutoMapper.TypePair, AutoMapper.TypePair)' to access method 'AutoMapper.MapperConfiguration+<>c__DisplayClass69_0.b__0(AutoMapper.ProfileMap)' failed.

This is a method access exception but I don't know how to resolve the problem. Can someone give me insight to what is happening inside there?

1 Answers1

1

I'm also getting the same issue, and I'm using .NET 4.5.

After some searching and trail and error I found that the issue is with System.ValueTuple So, I then downgraded to Automapper 6.22 (which don't have a dependency on System.ValueTuple) and now I'm able to configure it and everything working fine.

Kiran Marturu
  • 115
  • 2
  • 4
  • 12
  • Maybe, but that's a problem with the .Net framework itself and can be fixed using some binding redirect, as some research will show. – Lucian Bargaoanu Sep 10 '18 at 10:49
  • @LucianBargaoanu I'm using VS2012 and I see that in System.ValueTuple 4.5.0 package I don't see any folder net45 (whereas it shows no dependencies for .net 4.5 in the nuget page). So, I used the Portable Class Library version. May be something to do with it. I'm new to .NET world. So, I took the easiest way out. If any other way just guide me plz. – Kiran Marturu Sep 10 '18 at 10:58