Answer the question
In order to leave comments, you need to log in
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; }
}
CreateMap<TeacherEntity, TeacherDTO>();
CreateMap<ClassroomEntity, ClassroomDTO>();
CreateMap<TeacherClassroomEntity, ClassroomDTO>()???
вот так: .ForMember(dest => dest, opt => opt.MapFrom(source => source.Classroom)? - ругается.
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question