EF Core with AutoMapper

132 Views Asked by At

I have a DTO containing properties and a Model, eg student can have more than one module and a module can be associated with more than one student. the properties are mapping fine but the Model doesn't map.

 public class GetStudentByIdMapping : Profile
{
    public GetStudentByIdMapping()
    {
        CreateMap<Student,StudentDetails>();
        CreateMap<Module, StudentDetails>()
            .ForPath(dest => dest.StudentModules.ModuleName, opt => opt.MapFrom(m => m.ModuleName))
            .ForPath(dest => dest.StudentModules.ModuleCode, opt => opt.MapFrom(m => m.ModuleCode))
            .ForPath(dest => dest.StudentModules.Description, opt => opt.MapFrom(m => m.Description))
            .ReverseMap();


    }
}


    public async Task<StudentDetails> GetStudent(int studentId)
    {
        var student = context.Student
                             .Where(s => s.StudentId == studentId)
                             .FirstOrDefault();

        var module = await context.Order
                           .Include(m => m.Module)
                           .Where(o => o.StudentId == studentId)
                           .Select(m => m.Module).ToListAsync();


        var studMap =  Mapper.Map<StudentDetails>(student);
        Mapper.Map<StudentDetails>(module);


        return studMap;
    }

These are the ViewModels I want to map to the Models Model in the StudentDetails ViewModel

public class StudentDetails
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
    public string PhoneNumber { get; set; }

    public StudentModule StudentModules { get; set; }

}
public class StudentModule
{
    public string ModuleName { get; set; }
    public string ModuleCode { get; set; }
    public string Description { get; set; }

}

These are my Entities generated by EF Core

public partial class Student
{
    public Student()
    {
        Order = new HashSet<Order>();
    }

    public int StudentId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
    public string PhoneNumber { get; set; }
    public string Username { get; set; }
    public string Password { get; set; }

    public virtual ICollection<Order> Order { get; set; }
}

public partial class Module
{
    public Module()
    {
        Order = new HashSet<Order>();
    }

    public int ModuleId { get; set; }
    public int? LectureId { get; set; }
    public string ModuleName { get; set; }
    public string ModuleCode { get; set; }
    public string Description { get; set; }
    public string ModulePath { get; set; }

    public virtual Lecture Lecture { get; set; }
    public virtual ICollection<Order> Order { get; set; }
}
1

There are 1 best solutions below

0
LouraQ On

You only need to pass the created destination to the second map call:

Just try the following code when mapping:

 var studMap = Mapper.Map<Student,StudentDetails>(student);
 Mapper.Map<Module,StudentDetails>(module,studMap);

Then the studMap will receive all mapped fields value.