Using AutoMapper to map a complex viewmodel to a model using a collection property

I have an EF EF generated Person class that has several properties. I also have a Jobs class with several properties. A person is associated with several tasks, and therefore, Jobs is a collection property of a person.

I created a view model as such:

public class PersonViewModel
{
    public Person Person{ get; set; }
    public List<Job> Jobs{ get; set; }
}

From my point of view, I am sending an instance of PersonViewModel. I would like to use AutoMapper to map this view model to an instance of Person with a task collection property populated with a list from the view model.

Can this be done? So far I have tried:

 Mapper.CreateMap<PersonViewModel, Person>();

no luck ...

EDIT:

OK, this really works. I found that I have a problem elsewhere ...

My Person one-many PersonType... PersonType Person, auto mapper ... ... PersonTypeID . , , . ...

2: , Person PersonTypeID ( PersonType)... Person as PersonType...

, , , , PersonTypeID Person ...

automapper, , PersonType nav Person ... AutoMapper.AutoMapperMappingException

Destination property: PersonType
Exception of type 'AutoMapper.AutoMapperMappingException' was thrown.
+5
2

, . Model ViewModel - - AutoMapper , :

  • , , , , mapboth
  • Job JobViewModel, ( ), .

, : , , :

Mapper.CreateMap<PersonViewModel, Person>();
Mapper.CreateMap<Person, PersonViewModel>();

ViewModel Model, , AutoMapper , . - :

Automapper:

:

http://lostechies.com/jimmybogard/2009/05/06/automapper-feature-custom-type-converters/

+8

:

Mapper.CreateMap<Person, PersonViewModel>().ReverseMap();
0

All Articles