I am trying to map objects to a collection through a table of many of many, using AutoMapper.
My domain model (Entity Framework) looks like this:
public class User
{
public int UserID { get; set; }
public string Name { get; set; }
public IList<UserCompany> UserCompanies { get; set; }
}
public class Company
{
public int CompanyID { get; set; }
public string Name { get; set; }
}
public class UserCompany
{
public int UserCompanyID { get; set; }
public int UserID { get; set; }
public int CompanyID { get; set; }
}
I am trying to map a class that looks like this:
public class CompanyDTO
{
public int CompanyID { get; set; }
public string Name { get; set; }
}
public class UserDTO
{
public int UserID { get; set; }
public string Name { get; set; }
public IList<CompanyDTO> Companies { get; set; }
}
My current mapping configuration looks like this:
Mapper.CreateMap<Company, CompanyDTO>();
Mapper.CreateMap<User, UserDTO>()
.ForMember( dto => dto.Companies,
opt => opt.MapFrom(x => Mapper.Map<IList<Company>, IList<CompanyDTO>>(x.UserCompanies.Select( y => y.Company ).ToList())));
Arguing that the configuration validly returns true, but when I try to actually map the user into UserDTO, I get: The signature of the body and the declaration in the implementation of the method does not match.
If I use AfterMap and manually move each company to the list of companies, it will work, but it seems to me that I can deal with this as part of creating a map.
In my query, to get one user from the database I including the UserCompany.Company navigation property and I can quickwatch and see what is returned.