0

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

Johny
  • 71
  • 6
  • 1
    pagedPersons.ListOfPersons.ToList() ? – Ross Bush Feb 02 '17 at 16:43
  • I tried that and have even tried using pagedPerson.ListOfPersons.ToList() but still the same error. – Johny Feb 02 '17 at 16:54
  • thanks Ross, your tip did help. Could you please post an answer and then I'll accept it. – Johny Feb 03 '17 at 17:25
  • I did post as answer. – Ross Bush Feb 03 '17 at 18:14
  • Doesn't make much sense to me. How can `ToList` essentially change anything for AutoMapper to be able to find the mapping? Does it actually fail in the code exactly as you post it under "Controllers"? – Gert Arnold Feb 03 '17 at 20:57
  • It was a dumb error on my side. I was testing in 2 different projects (viewModel_1 in one project and view_Model_2 in different project). viewModel_1 project was throwing error because I forgot to include the registerAutoMapper configuration within the global.asax. But while answering to Ross's comment, I remembered what could have gone wrong. So, from my point of view, Ross did help me. Thanks everyone. – Johny Feb 03 '17 at 22:48

2 Answers2

1

You need to return a List<> - pagedPersons.ListOfPersons.ToList()

Ross Bush
  • 14,648
  • 2
  • 32
  • 55
-1

try

var viewModel_1 = AutoMapper.Mapper.Map<IEnumerable<Person>, IEnumerable<PersonDetailViewModel>>(pagedPersons.ListOfPersons.ToList());
Bigeyes
  • 1,508
  • 2
  • 23
  • 42