Entity Model
public partial class Categoies
{
public Categoies()
{
this.Posts = new HashSet();
}
public int Id { get; se
In your class AutoMapper
profile, you need to create a map for your entity and viewmodel.
This is usually in AutoMapper/DomainToViewModelMappingProfile
In Configure()
, add a line like
Mapper.CreateMap<YourEntityViewModel, YourEntity>();
In ViewModelToDomainMappingProfile
, add:
Mapper.CreateMap<YourEntity, YourEntityViewModel>();
Gist example
I did this to remove the error:
Mapper.CreateMap<FacebookUser, ProspectModel>();
prospect = Mapper.Map(prospectFromDb, prospect);
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.
I know this is a rather old question as of now, but I had found the proper solution was that I was not declaring the assembly attribute.
My code is:
using AutoMapper;
...
namespace [...].Controllers
{
public class HousingTenureTypesController : LookupController<HousingTenureType, LookupTypeModel>
{
Mapper.CreateMap<HousingTenureType, LookupTypeModel>().ReverseMap();
}
...
}
This was fixed by adding the following line before my namespace declaration:
[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(HousingTenureTypesController), "AutoMapperStart")]
The full code is:
using AutoMapper;
...
[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(HousingTenureTypesController), "AutoMapperStart")]
namespace [...].Controllers
{
public class HousingTenureTypesController : LookupController<HousingTenureType, LookupTypeModel>
{
Mapper.CreateMap<HousingTenureType, LookupTypeModel>().ReverseMap();
}
...
}
I had same issue in .Net Core. Because my base dto class(i give it as a type in startup for automapper assembly) was in different project. Automapper tried to search for profiles in base class project. But my dto's were in different project. I moved my base class. And problem solved. This may help for some persons.