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?
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.