A
A
Anton Starostin2014-10-17 21:36:21
.NET
Anton Starostin, 2014-10-17 21:36:21

Why doesn't Automapper map collections of EF objects into collections of containers?

The database model includes 3 tables: Users, Media Materials, User Types.
Accordingly, the user entity refers to two other tables, forming links through the Navigation Property.
In my project, I use Automapper in the following way:
1. I created the CommonMapper class, which registers all maps in the constructor. For example like this

Mapper.CreateMap<MediaEntity, MediaContainer>()
Mapper.CreateMap<MediaContainer, MediaEntity>();
Mapper.CreateMap<IEnumerable<MediaContainer>, IEnumerable<MediaEntity>>();
Mapper.CreateMap<IEnumerable<MediaEntity>, IEnumerable<MediaContainer>>();

2. Next, it has a method. Here is his code
public DestinationType Map<SourceType, DestinationType> (SourceType source)
        {
            return Mapper.Map<SourceType, DestinationType>(source);
        }

3. In the code I use for example like this.
UsersContainer test = _mapper.Map<UsersEntity, UsersContainer>(entity);

The problem is that one object from EF is quietly mapped into a DTO container, but if the objects are in the collection, from using
_mapper.Map<IEnumerable<UsersEntity>,IEnumerable<UsersContainer>>(entities);

or even so
_mapper.Map<UsersEntity,UsersContainer>(entities);
I always get the same exception. Here is the outer one:
Mapping types:
IEnumerable`1 -> IEnumerable`1
System.Collections.Generic.IEnumerable`1 -> System.Collections.Generic.IEnumerable`1

Destination path:
IEnumerable`1

Source value:
System.Collections.Generic.List`1[InstaBot.DAL.Entities.UsersEntity]

And here is the InnerException:
Отсутствует реализация метода "GetEnumerator" в типе "Proxy<System.Collections.Generic.IEnumerable`1_mscorlib_Version=4.0.0.0_Culture=neutral_PublicKeyToken=b77a5c561934e089>" из сборки "AutoMapper.Proxies, Version=0.0.0.0, Culture=neutral, PublicKeyToken=be96cd2c38ef1005".

I have already tried to create a map in a different way. For example like this:
Mapper.CreateMap<UsersEntity, UsersContainer>()
                .ForMember(dto => dto.UserTypes, opt => opt.MapFrom(x => x.UserTypes))
                .ForMember(dto => dto.Media, opt=>opt.MapFrom(x=>x.Media));

However, this did nothing. The most interesting thing is that an object maps into a container without problems, but a collection of objects into a collection of containers does not. Please help me to solve this problem. For a day now I can not understand anything.
PS: I'm not sure if the problem is because of the Navigation Property, just to clarify just in case. Probably all the same, I'm using the automapper incorrectly. But mapping one entity in a loop, when there are from 1k to 50k of them, is not an option at all...

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
mayorovp, 2014-10-20
@mayorovp

Mapper.CreateMap<IEnumerable<MediaContainer>, IEnumerable<MediaEntity>>();
Mapper.CreateMap<IEnumerable<MediaEntity>, IEnumerable<MediaContainer>>();
Remove these lines first. Automapper will figure out what to do with enums.

R
Rayvor, 2018-09-10
@Rayvor

Mapper.Initialize(cfg => cfg.CreateMap<UsersEntity, UsersContainer>));

var users = _mapper.Map<IEnumerable<UsersContainer>, List<UsersEntity>>(entities);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question