0

I'm very new to Automapper and I'm facing difficulties in Updating mapped fields. I must agree that there are millions of answers for this question in internet but the problem is nothing helps for my scenario.

Issue: I'm trying to update only the mapped fields. But when I do that, my complete destination properties gets updated.

For e.g.,

My destination object(DB Table) holds 5 properties and all the 5 properties accepts null. But I have only 3 properties in my source(Model file). I'm mapping all the three source fields with the destination fields but I don't have mapping for the remaining two destination properties. You would have guessed it correctly, yes when I update this, that two unmapped fields are also getting update to null(default value - DB). But it should not be the case, rather it should have the existing values.

Kindly see my code below,

References: Automapper Version: v6.2.2 and Runtime Version: v4.0.3

Repository.cs,

var objectToUpdate = Mapper.Map<TDomain>(entity); //TDestination Map<TDestination>(object source);
DatabaseContext.Entry(objectToUpdate).State = EntityState.Modified;

InvoiceLineMapping.cs

public static void Map(IProfileExpression profile)
    {                     
                profile.CreateMap<Registration, PT_Registration>()
                .ForMember(d => d.Name_VC, map => map.MapFrom(s => s.Name))
                .ForMember(d => d.UserName_VC, map => map.MapFrom(s => s.Username))
                .ForMember(d => d.Mobile_VC, map => map.MapFrom(s => s.Mobile));
     }

Note: As I said before, I have two unmapped fields are there in the destination and they are EMail_VC and Comments_VC and their values are sample@test.com and testComments respectively. When update happens, these two fields becomes null(default value)

I tried,

  • .ForAllOtherMembers(opt => opt.Ignore()); //after the last mapping i.e Mobile
  • profile.CreateMap(MemberList.Source) // I also tried MemberList.Destination
  • I also tried, AutoMapper: "Ignore the rest"? extenstion method,
  • I tried puttin ReverseMap() in the end. //after the last mapping i.e Mobile
  • I even tried to find the null value and removing from destination object(objectToUpdate - you can see above) but nothing helps.

So can someone please look into this and provide me a better solution. I knew solution would be simpler but as I'm not aware of it, it takes more and more time.

I appreciate your time. Any help would be great helpful.

gkrishy
  • 756
  • 1
  • 8
  • 33
  • Use the `Map` overload that allows you to pass an existing destination object. – Lucian Bargaoanu Sep 30 '19 at 10:12
  • I believe Lucian is correct. The method you are using to map is one that takes a model and returns a NEW instance of the destination model. There are many overloads that either return a new instance or will take an existing instance and map properties onto that according to your map configuration. I'd expect your problem to be solved if you used the method that looks like... Mapper.Map(srcObj, destObj) <== this takes an existing instance of the destination and will apply your mapping to it. – kd345205 Oct 09 '19 at 20:56

0 Answers0