I'm using Automapper to copy one object properties to other and later will update in database using EF.
Question is how to tell Automapper copy every property but ignore a particular property (in this case it will be Id). I'm new to AutoMapper and just have done this code. I don't have other configurations or use of AutoMap in project.
Mapper.Map(lead, existingLead);
I have downloaded AutoMapper form here https://github.com/AutoMapper/AutoMapper
On your Mapper.CreateMap<Type1, Type2>()
you can use either
.ForSourceMember(x => x.Id, opt => opt.Ignore())
or
.ForMember(x => x.Id, opt => opt.Ignore())
UPDATE:
It seems like .Ignore()
is renamed to .DoNotValidate()
according to the AutoMapper docs.
I use this extension method:
public static IMappingExpression<TSource, TDestination> IgnoreMember<TSource, TDestination>(
this IMappingExpression<TSource, TDestination> map, Expression<Func<TDestination, object>> selector)
{
map.ForMember(selector, config => config.Ignore());
return map;
}
and I use it like this
Mapper.CreateMap<MyType1, MyType2>().IgnoreMember(m => m.PropertyName);
Hope that helps.