I’m using NHibernate (v3.3) to load/save my domain model classes to database. I’ve created bunch of View Model classes to use on front end of the MVC3 website. So I was planning to use AutoMapper (v2.1) to map view model classes to domain classes.
I’ve define Map configuration between two classes Restaurant and RestaurantViewModel. Here is the method:
public void Create(IConfiguration configuration)
{
if (configuration == null)
throw new ArgumentNullException("configuration");
IMappingExpression<RestaurantViewModel, Restaurant> map =
configuration.CreateMap<RestaurantViewModel, Restaurant>();
map.ForMember(x => x.Address, o => o.ResolveUsing(x => new Address
{
BuildingNumber = x.BuildingNumber,
City = x.City,
PostalCode = x.PostalCode,
Street = x.Street
}));
map.ForMember(x => x.Categories, o => o.Ignore());
map.ForMember(x => x.Photo, o => o.Ignore());
}
But I got exception when calling Mapper.Map<RestaurantViewModel>(restaurant);
This is the text of exception:
Missing type map configuration or unsupported mapping.
Mapping types:
RestaurantProxy -> RestaurantViewModel
RestaurantProxy -> FoodDelivery.Website.Models.RestaurantViewModel
Destination path:
RestaurantViewModel
Source value:
FoodDelivery.DataDomain.Restaurant
It looks like NHibernate create proxy around my Restaurant with name RestaunrantProxy, so when I ask AutoMapper to map Restaurant to RestaurantViewModel it actually maps Proxy and because there is no Map for that mapper throws an exception.
Are there any ways I can fix the code?
It looks like your error is having problems mapping the
Restaurantto theRestaurantViewModel. Your mapping only appears to cover the mapping in the other direction, view model to entity.Try adding this?
That should allow you to map your Restaurant entity to your view model.