K
K
Kolejium2018-12-17 18:28:21
C++ / C#
Kolejium, 2018-12-17 18:28:21

How to Automapper createmap without destination or AutoMapper and Many-to-Many?

Hi all!
Need help with mapping)
There are three entities and 2 DTOs - their components do not affect the strong role, but the meaning is in the relationship.

public class ClassroomDTO
    {
        public Guid Id { get; set; }
        public byte Number { get; set; }
        public char Letter { get; set; }
        public IList<TeacherDTO> Teachers { get; set; }
    }
    public class TeacherDTO
    {
        public Guid Id { get; set; }
        public string Name { get; set; }
        public string Surname { get; set; }
        public byte Age { get; set; }
        public string Position { get; set; }
        public IList<ClassroomDTO> Classrooms { get; set; }
    }
public ClassroomEntity : BaseEntity 
{
        public ClassroomEntity() : base()
        {
            Teachers = new List<TeacherClassroomEntity>();
        }
        public byte Number { get; set; }
        public char Letter { get; set; }
        public virtual IList<TeacherClassroomEntity> Teachers { get; set; }
}

public TeacherEntity : BaseEntity 
{
        public TeacherEntity() : base()
        {
            Classrooms = new List<TeacherClassroomEntity>();
        }

        public string Name { get; set; }
        public string Surname { get; set; }
        public byte Age { get; set; }
        public EPosition Position { get; set; }
        public virtual IList<TeacherClassroomEntity> Classrooms { get; set; }
}

    public class TeacherClassroomEntity
    {
        public Guid TeacherId { get; set; }
        public Guid ClassroomId { get; set; }
        public TeacherEntity Teacher { get; set; }
        public ClassroomEntity Classroom { get; set; }
    }

For example, I get teachers, something like this: _uow.Teachers.Entities.Include(x => x.Classrooms).ThenInclude(x => x.Classroom); - complete information about the teacher including the classes in which he leads.
Now the maps themselves:
Something like this:
CreateMap<TeacherEntity, TeacherDTO>();
CreateMap<ClassroomEntity, ClassroomDTO>();

And here the question is how to register such a map:
CreateMap<TeacherClassroomEntity, ClassroomDTO>()???
вот так: .ForMember(dest => dest, opt => opt.MapFrom(source => source.Classroom)? - ругается.

So how to be? Because EF works well, but I can't map it.
PS If possible, then with an explanation) And if everything is fundamentally wrong, please give a couple of examples!)
PS ForMemeber - I know about each property, but I have overloaded DTOs (the so-called EntityStatsDTO - which consist of a very large pile of fields)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Kolejium, 2018-12-17
@kolejium

Guys, nevertheless, I managed to solve it myself, at the bottom of the coder, but if you know a better way, please share!) I would be very grateful !!!

CreateMap<TeacherEntity, TeacherDTO>()
                .ForMember(dest => dest.Classrooms, opt => opt.MapFrom(source => source.Classrooms.Select(x => x.Classroom).ToList()));
            CreateMap<ClassroomEntity, ClassroomDTO>()
                .ForMember(dest => dest.Teachers, opt => opt.MapFrom(source => source.Teachers.Select(x => x.Teacher).ToList()));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question