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.