Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignore a property in AutoMapper?

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

like image 983
user576510 Avatar asked Nov 12 '14 23:11

user576510


1 Answers

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.

like image 106
John Avatar answered Oct 08 '22 16:10

John