I am new to AutoMapper and using version 6.2.2. I am trying to map a view model to an entity (also using Entity Framework). I want to update only the properties that exist in both the viewmodel and the entity. The entity has other navigational properties and related objects that are not part of the source viewmodel. I am currently getting an error that I have unmapped properties on the destination entity. Both my viewmodel and entity have over 40 properties so I do not want to explicitly add each one to the map.
Here is my code:
Map:
public static void RegisterMaps()
{
AutoMapper.Mapper.Initialize(config =>
{
config.CreateMap<EditApplicationViewModel, Application>();
});
}
I have also tried the following but get the same error:
config.CreateMap<EditApplicationViewModel, Application>(MemberList.source);
Controller:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(EditApplicationViewModel viewModel)
{
if (ModelState.IsValid)
{
Application application = _applicationService.GetById(viewModel.ApplicationId);
application = Mapper.Map(viewModel, application);
}
}
Error Message:
InnerException: HResult=-2146233088 Message= Unmapped members were found. Review the types and members below. Add a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type For no matching constructor, add a no-arg ctor, add optional arguments, or map all of the constructor parameters ========================================================== String -> User (Destination member list) System.String -> ..***.entities.User (Destination member list)
Unmapped properties: removed - a very long list of related objects and properties on the destination
Source=AutoMapper StackTrace: at lambda_method(Closure , EditApplicationViewModel , Application , ResolutionContext )
UPDATE:
I have also tried the following map. I am not receiving any errors but none of the source properties are updated on the destination.
config.CreateMap<EditApplicationViewModel, Application>().ForAllOtherMembers(opts=>opts.Ignore());