1

I have a basic table with a few FK references. So when I retrieve an entity for an update operation; that entity contains ICollectionsof related entites. I also have a main ViewModel inside which each entity is a sub viewModel. My aim is to use Automapper like:

mapper.Map(MainViewmodel obj,MainEntity obj); 

For this I have a MappingConfiguration like:

 CreateMap<MainViewModel, MainEntity>();
 CreateMap<subViewModel1, subEntity1>();
 CreateMap<subViewModel2, subEntity2>();

This gives me an Unmapped properties exception because the ICollections are not getting mapped. Could anyone please guide me on the best way to deal with this kind of situation?

thanks.

Shwrk
  • 173
  • 1
  • 11
  • hi..there is a correction. It does not throw a exception(that was my mistake in passing argument).. at the same time, it does not map as well – Shwrk Dec 21 '17 at 12:44

1 Answers1

0

If I understand you correctly, you has classes like this:

class MainViewModel
{
    ICollection<SubViewModel1> SubViewModels { get;set; }
}

class SubViewModel1
{
}

and

class MainEntity
{
    ICollection<SubEntity1> SubEntities { get;set; }
}

class SubEntity1
{    
}

Then you should create rules for each class, and collection of such classes automaper map automatically.

CreateMap<MainViewModel, MainEntity>();
CreateMap<SubViewModel1, SubEntity1>();

Addditions 1:

  1. Try this method: var mappedMainEntity = rmapper.Map<MainEntity>(MainViewmodel obj);
  2. If you map MainEntity to MainViewModel you need add .ReverseMap() to map rule, like this:

    CreateMap().ReverseMap(); CreateMap().ReverseMap();

Additions 2:

  1. AutoMapper mapping public Properties only
  2. If the mapping Properties have different names, you need to use explicitly indicating how to map these Properties. Using ForMember method and MapFrom option. Example
Anton Gorbunov
  • 1,414
  • 3
  • 16
  • 24
  • hi @Anton Gorbunov in MainViewModel.. the subViewModel is not declared as an `ICollection` . I have used `List` – Shwrk Dec 21 '17 at 12:49
  • Hi @Shwrk. Typically, for the automapper, the collection type does not matter. Please, see and try additions in answer – Anton Gorbunov Dec 21 '17 at 12:53
  • hmm.. @Anton Gorbunov it didn't work mate, the `mappedMainEntity` turned out empty – Shwrk Dec 21 '17 at 13:04
  • good news :) It works when I specify `ForMember` property for each `ICollection`.. values are mostly null though which I think is some naming issue.. will check it out. thanks for your help – Shwrk Dec 21 '17 at 13:41
  • I'm sorry. I tried to write the code for an example and realized that I forgot to mention two important features. =)) 1. AutoMapper mapping public Properties only 2. If the Properties have different names, you need to use explicitly indicating how to create these Properties using ForMember method and MapFrom option – Anton Gorbunov Dec 21 '17 at 13:43
  • Example =) https://gist.github.com/Asc0tt/409473b2716c93bf39026694e899592a – Anton Gorbunov Dec 21 '17 at 13:50
  • np mate :) I have 1 more issue.. In the main Entity.. I have some properties in addition to `ICollections`. These properties are mapped in a subViewModel from View. So when I write `CreateMap()`, these fields don't get mapped to entity because they inside of a subViewModel. So I am currently writing `ForMember` for each such field(around 20). Is there a better way to do it? – Shwrk Dec 21 '17 at 13:52