Automapper missing type map configuration or unsupported mapping - Error
I was trying to map an IEnumerable to an object. This is way I got this error. Maybe it helps.
Where have you specified the mapping code (CreateMap)? Reference: Where do I configure AutoMapper?
If you're using the static Mapper method, configuration should only happen once per AppDomain. That means the best place to put the configuration code is in application startup, such as the Global.asax file for ASP.NET applications.
If the configuration isn't registered before calling the Map method, you will receive Missing type map configuration or unsupported mapping.
Notice the Categoies_7314E98C41152985A4218174DDDF658046BC82AB0ED9E1F0440514D79052F84D
class in the exception? That's an Entity Framework proxy. I would recommend you disposing of your EF context to ensure that all your objects are eagerly loaded from the database and no such proxies exist:
[HttpPost]
public ActionResult _EditCategory(CategoriesViewModel viewModel)
{
Categoies category = null;
using (var ctx = new MyentityFrameworkContext())
{
category = ctx.Categoies.Find(viewModel.Id);
}
AutoMapper.Mapper.Map<CategoriesViewModel, Categoies>(viewModel, category);
//category = AutoMapper.Mapper.Map<CategoriesViewModel, Categoies>(viewModel, category);
entity.SaveChanges();
}
If the entity retrieval is performed inside a data access layer (which of course is the correct way) make sure you dispose your EF context before returning instances from your DAL.
In your class AutoMapper
profile, you need to create a map for your entity and viewmodel.
ViewModel To Domain Model Mappings:
This is usually in AutoMapper/DomainToViewModelMappingProfile
In Configure()
, add a line like
Mapper.CreateMap<YourEntityViewModel, YourEntity>();
Domain Model To ViewModel Mappings:
In ViewModelToDomainMappingProfile
, add:
Mapper.CreateMap<YourEntity, YourEntityViewModel>();
Gist example