I'm using AutoMapper 4.1.1 within MVC 5 and then using it to map the entities from EF6 (pure Poco with any virtual and no lazy loading) to viewModels. I'm having an strange issue where AutoMapper is translating it correctly but in the other instance is failing.
Dto.Person
{
public int PersonId { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public string Phone { get; set; }
}
ViewModel.PersonDetailViewModel
{
public int PersonId { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public string Phone { get; set; }
}
public class PagedPersons
{
public int TotalNumberOfRecords { get; set; }
public IEnumerable<Person> ListOfPersons { get; set; }
}
Function to fetch the Persons
public IEnumerable<Person> GetAllPersons_NOPaging()
{
IEnumerable<Person> persons = new List<Person>();
persons = GetFromdatabase();
return persons;
}
public PagedPersons GetAllPersons_WithPaging()
{
int totalNumberOfRecords = 0;
IEnumerable<Person> = GetFromDatabase();
return new PagedPersons { TotalNumberOfRecords = totalNumberOfRecords, ListOfPersons = listofPersons };
}
Controllers
var pagedPerson = GetAllPersons_WithPaging();
var persons = GetAllPersons_NOPaging();
AutoMapper.Mapper.CreateMap<Person, PersonDetailViewModel>();
// THROWS AN ERROR
var viewModel_1 = AutoMapper.Mapper.Map<IEnumerable<Person>, IEnumerable<PersonDetailViewModel>>(pagedPersons.ListOfPersons);
// does NOT throw error
var viewModel_2 = AutoMapper.Mapper.Map<IEnumerable<Person>, IEnumerable<PersonDetailViewModel>>(persons);
So, I fail to understand what I'm doing wrong as AutoMapper is able to map correctly in case of viewmodel_2 but throwing the following error on viewModel_1.
Missing type map configuration or unsupported mapping.
Mapping types: Person -> PersonDetailViewModel MyProject.Dto.Person -> MyProject.Website.ViewModels.PersonDetailViewModel
Destination path: IEnumerable`1[0]
Source value: MyProject.Dto.Person
You need to return a List<> - pagedPersons.ListOfPersons.ToList()