Reputation: 341
I am using EF Code First and defined a number of POCOs. On the other hand, There are some ViewModel classes defined for each of which. Up to here, there are two projects; Model and MVC. Moreover, there is another project containing UnitOfWork and Repository Pattern(a generic repository class).
In the UnitOfWork a number of fields and properties written. Here is an example:
public GenericRepository<ActionProvider> ActionProviderRepository
{
get
{
if (this.actionProviderRepository == null)
{
this.actionProviderRepository = new GenericRepository<ActionProvider>(context);
}
return actionProviderRepository;
}
}
The type of this property is that of ActionProvider class defined in the Model project.
But my problem; I need to Map each of the ViewModel to the POCO classes. After surfing the web, I've come to apply AutoMapper. So the following is what I have done in the UserController class:
public class UserController : Controller
{
private UnitOfWork unitOfWork = new UnitOfWork();
private User _UserModel = new User();
private VM_User _VM_User = new VM_User();
//
// GET: /User/
public ViewResult Index()
{
_VM_User = Mapper.Map<MAHAL_E_MA_Model.POCO.User, VM_User>(_UserModel);
return View(unitOfWork.UserRepository.Get(orderBy: us => us.OrderByDescending(u => u.ID)).OfType<User>().ToList());
}
}
And the error is:
Missing type map configuration or unsupported mapping.
I have tried through these links and some other ones but did't work for me! This one
Any help would be kindly appreciated..
Upvotes: 0
Views: 525
Reputation: 341
I finally overcome the matter by the following lines of code and want to share it perhaps be useful for others:
public ViewResult Index()
{
var query = unitOfWork.UserRepository.Get(orderBy: us => us.OrderByDescending(u => u.ID)).OfType<User>().ToList();
Mapper.CreateMap<User, VM_User>();
IList<VM_User> vm_Users = Mapper.Map<IList<User>, IList<VM_User>>(query);
return View(vm_Users);
}
Upvotes: 1
Reputation: 1620
You need to create the map somewhere
Mapper.CreateMap<MAHAL_E_MA_Model.POCO.User, VM_User>();
I have a Mapping.Setup()
method in Application_Start
that creates all the maps needed.
Upvotes: 3