0

I have an entity and a corresponding DTO

public class PersonEntity {
    public int personId;
    public List<Contact> contacts;
}
public class PersonDto {
   public int personId;
   public List<int> contacts;
}

Using the following map with AutoMapper

Mapper.Map<PersonDto, Person>();

I'm using AutoMapper to get the DTO, which isn't a problem.

I'm parsing the DTO back to the Entity, to update fields in the Entity for a save operation and I'm not really interested in the list of contacts anymore. Automapper throws an exception with this as it doesn't like mapping the list of int's to a list of objects.

any suggestions or better ways to do this please.

Edit

solution used is

Mapper.CreateMap<PersonDto, Person>()
            .ForMember(x => x.contacts, y => y.Ignore());
Craig
  • 381
  • 5
  • 22

2 Answers2

1

Can you use the ignore method in configuration?

http://automapper.codeplex.com/wikipage?title=Configuration%20Validation

opt => opt.Ignore()

But. Do you really need to update the entity just to save? Why don't you send a command which contains the changed data.

  • This worked great. I'm marking the DTO as dirty or new and depending on these flags, I will then update the entity via the automapper, then save the entity via the repository and therefore omitting the need to write an update method for 100's of properties on all of the entities. Thanks – Craig Mar 06 '12 at 22:03
0

AutoMapper as you may know, looks for properties in objects with the same name & type, therefore you will most likely need to alter your DTO or Entity so they match.

LenPopLilly
  • 1,227
  • 3
  • 13
  • 21
  • I don't want to recreate the whole hierarchy of object relations in the DTO mappings, this would be overkill for what I'm trying to achieve. opt.Ignore() worked – Craig Mar 06 '12 at 22:05