Is there some way, e.g. maybe an attribute I’m not aware of, to prevent properties from being applied by the ApplyCurrentValues method? In my currently applicable case, I am working with a Member entity and an Edit view in an MVC3 application, and properties such as CreateDate don’t get changed by the Edit action. I have a view model, MemberDetailModel used for the Edit action, which has a MapToEntity method, but this method returns a new entity, using the following AutoMapper invocation.
public virtual TEntity MapToEntity()
{
return (TEntity)Mapper.Map(this, GetType(), typeof(TEntity));
}
As I see things now, I have to use some reflection and iterative property assignment, either from the view model to a new EM object, and still use ApplyCurrentValues, between the EM object returned by MapToEntity in my own update method, e.g. ApplyCurrentValuesWithExceptions.
ApplyCurrentValues= apply all values (except navigation properties) from detached entity to attached entity. You can’t apply only subset of values.The simplest way is simply create specific
MapToEntitymethod which will receive attached instance of your entity and copy fields from your view model to the entity one by one (or with some automation). Creating detached instance with AutoMapper is redundant because you don’t need to have view model, detached entity instance and attached entity instance. Using this approach with AutoMapper makes sense if you don’t have attached entity and you want to transform your view model into entity which will be attached to context.