Automapper - can it map over only existing properties in source and destination objects?
You can explicitly tell AutoMapper
to Ignore
certain properties if required like this:
Mapper.CreateMap<Users, tblUserData>() .ForMember(dest => dest.Id, opt => opt.Ignore());
which will mean the Id column in the destination object will ALWAYS be left untouched.
You can use the Condition
option to specify whether or not a mapping is applied depending on the result of a logical condition, like this:
Mapper.CreateMap<Users, tblUserData>() .ForMember(dest => dest.Id, opt => opt.Condition(src=>src.Id.HasValue));
or
Mapper.CreateMap<Users, tblUserData>() .ForMember(dest => dest.Id, opt => opt.Condition(src=>src.Id != null));
depending on your specific requirements.
Instead of
userData = _mapper.Map<Users, tblUserData>(user);
use
_mapper.Map(user, userData);
The former creates a new userData object with only the properties available in user object. The later overwrites the existing userData object's properties with properties present in user object, while retaining the rest.
You can tell AutoMapper to ignore fields that you do not want mapped like this:
userData = Mapper.Map<Users, tblUserData>(user).ForMember(m => m.EntityKey, opt => opt.Ignore());