Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automapper IEnumerable<something> to IEnumerable<somethingelse> without creating a map configuration

I cannot get the following to work, where array is an array of CustomerContract's:

Mapper.Map<IEnumerable<Customer>>(array);

Mapper.Map<IEnumerable<CustomerContract>, IEnumerable<Customer>>(array);

Mapper.Map<Array, List<Customer>>(array);

In my mind the first example should be enough, but i can not get either to work. I have read the configuration wiki of automapper (https://github.com/AutoMapper/AutoMapper/wiki/Configuration), but i do not understand why this should be necessary. Everything Automapper needs is defined in the command. Which type it is (both object and that it is a list), and which object i want it to map to.

Am i just not understanding the core concept of Automapper?

My exception sounds like this:

Missing type map configuration or unsupported mapping.
Mapping types:\r\nCustomerContract -> Customer\r\nStimline.Xplorer.Repository.CustomerService.CustomerContract -> Stimline.Xplorer.BusinessObjects.Customer
Destination path: List`1[0]
Source value: Stimline.Xplorer.Repository.CustomerService.CustomerContract

like image 957
Bjørn Avatar asked Jun 11 '14 06:06

Bjørn


People also ask

How do I use AutoMapper to list a map?

How do I use AutoMapper? First, you need both a source and destination type to work with. The destination type's design can be influenced by the layer in which it lives, but AutoMapper works best as long as the names of the members match up to the source type's members.

Where is AutoMapper configuration?

Where do I configure AutoMapper? ¶ Configuration should only happen once per AppDomain. That means the best place to put the configuration code is in application startup, such as the Global.

Does AutoMapper map both ways?

Yes, or you can call CreateMap<ModelClass, ViewModelClass>(). ReverseMap() .


1 Answers

You're mapping to IEnumerable... Automapper can map to a concrete type not an interface.

First register your mapping (see the "Missing type map configuration or unsupported mapping") You must use CreateMap once for performance

Mapper.CreateMap<something, somethingelse>();

Instead of:

Mapper.Map<IEnumerable<Customer>>(array);

Try this:

Mapper.Map<List<Customer>>(array);

or

Mapper.Map<Customer[]>(array);
like image 105
JuChom Avatar answered Sep 21 '22 16:09

JuChom