Answer the question
In order to leave comments, you need to log in
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>>();
public DestinationType Map<SourceType, DestinationType> (SourceType source)
{
return Mapper.Map<SourceType, DestinationType>(source);
}
UsersContainer test = _mapper.Map<UsersEntity, UsersContainer>(entity);
_mapper.Map<IEnumerable<UsersEntity>,IEnumerable<UsersContainer>>(entities);
_mapper.Map<UsersEntity,UsersContainer>(entities);
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]
Отсутствует реализация метода "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".
Mapper.CreateMap<UsersEntity, UsersContainer>()
.ForMember(dto => dto.UserTypes, opt => opt.MapFrom(x => x.UserTypes))
.ForMember(dto => dto.Media, opt=>opt.MapFrom(x=>x.Media));
Answer the question
In order to leave comments, you need to log in
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.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question