2
votes

I am trying to map multiple domain models to one view model. I found this link which is able to do just that. However, I have several domain model properties that do not exist in the view model. When I try to map the models, I am getting the error:

Missing type map configuration or unsupported mapping.

This is because I am not ignoring the other properties. All the answers I've seen mention using option.Ignore() on the Mapper.CreateMap function. However, in the link I provided, the Mapper.CreateMap function is not used. The only call to AutoMapper is:

    Mapper.Map(source, destination, sourceType, destinationType);

How can I do the ignore in this case? Or is there a better way to do both mapping multiple models and ignoring properties?

1

1 Answers

5
votes

You should have a Mapper.CreateMap in your Application_Start for all the objects you are dealing with:

Mapper.CreateMap<SourceDomain1, MyViewModel>();
Mapper.CreateMap<SourceDomain2, MyViewModel>();
Mapper.CreateMap<SourceDomain3, MyViewModel>();

The fact that you have properties in your domain models which are not present in your view model is not a problem at all. They won't be taken into account. On the other hand you could use the .Ignore() method when configuring your AutoMapper to indicate that some property present in both the source and the target should be ignored.