0

I'm using automapper to map values from one object to another and then saving the object to a db using ef6. I would like Automapper to ignore any null values in the base so that the EF doesn't overwrite the original stored value with null. I've tried the below code but it seems the properties are set as null as the values are removed when stored in the db.

Any ideas?

cfg.CreateMap<BidBase, ProjectBase>()
                .ForMember(dest => dest.RambollProjectStartDate, opt => opt.MapFrom(src => src.ExpectedProjectStartDate))
                .ForAllMembers(opt => opt.Condition((source, destination, sourceMember) => sourceMember != null));

Update

I've realised that the problem is because automapper sets the properties of the destination object to null if they do not exist in the source object. This means that the value in the db is being set to null by the EF. How can I handle this?

halfer
  • 19,824
  • 17
  • 99
  • 186
Richard Banks
  • 2,946
  • 5
  • 34
  • 71
  • Which `Map` overload are you using? I don't think AM does anything with destination for properties that does not exist in the source. Can you show an example? – Ivan Stoev Mar 27 '17 at 10:49

1 Answers1

0

Try use conditional mapping:

Community
  • 1
  • 1
gabba
  • 2,815
  • 2
  • 27
  • 48