Click here to Skip to main content
14,732,983 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have an index view that routes to the details view for each student via StudentId. The details view displays a partial view of the student's course information (also routed via StudentId). The course information currently comes from two tables: StudentCourse and StudentCoursePast. 

I would like to map a third table - Course - so I can get the corresponding CourseTitle for each CourseId in my StudentCourseViewModel. With the code below, I am able to get the correct data from both studentCourse and StudentCoursePast tables. However, all my attempts to map the Course table so I can get the CourseTitle field in my view has been futile.Please help! 


What I have tried:

Student
public string StudentId
public string Name

StudentCourse
public string StudentId
public string Courseid
public string Room
public DateTime Date

StudentCoursePast
public string StudentId
public string CourseId
public string Room
public DateTime Date

Courses
Public string CourseId
Public string CourseTitle

StudentCourseViewModel
public string Courseid
public string CourseTitle
public string Room
public DateTime Date

AutoMapperProfile
CreateMap<StudentCourse, StudentCourseViewModel>
CreateMap<StudentCoursePast, StudentCourse>
CreateMap<Course, StudentCourseViewModel>.ForMember(dest => dest.Courseid, opts => opts.MapFrom(src => src.CourseId));

StudentController
var course = _context.StudentCourses.Where (s => s.StudentId == student.StudentId).ToList();
var coursePast = _context.StudentCoursesPast.Where (s => s.StudentId == student.StudentId).ToList();
var studentCourse = course.Union(_mapper.Map<List<StudentCoursePast>, List<StudentCourse>>(coursePast)).ToList();

model.StudentCourse=_mapper.Map<IList<StudentCourse>, IList<StudentCourseViewModel>>(studentCourse);
Posted
Updated 7-Aug-20 0:57am
v2

1 solution

Assuming you have navigation properties in place on the database entities, you should be able to do:
CreateMap<StudentCourse, StudentCourseViewModel>()
    .ForMember(dest => dest.CourseTitle, opts => opts.MapFrom(src => src.Course.CourseTitle));

CreateMap<StudentCoursePast, StudentCourseViewModel>()
    .ForMember(dest => dest.CourseTitle, opts => opts.MapFrom(src => src.Course.CourseTitle));
Usage:
var courses = _context.StudentCourses
    .Where(c => c.StudentId == student.StudentId)
    .ProjectTo<StudentCourseViewModel>();

var pastCourses = _context.StudentCoursesPast
    .Where(c => c.StudentId == student.StudentId)
    .ProjectTo<StudentCourseViewModel>();

model.StudentCourse = courses.UnionAll(pastCourses).ToList();
   
Comments
Member 14908535 7-Aug-20 12:56pm
   
Thanks for your help @Richard Deeming. StudentCourse and StudentCoursesPast do not contain the field CourseTitle, just CourseId. Only the Course entity has CourseTitle (as well as CourseId) which is what I am trying to get. With my current code, I am able to get every other field in the StudentCourseViewModel to display in my view except for CourseTitle. Your solution is somewhat similar to what I have now but still excludes CourseTitle from Course entity. Any further assistance will be appreciated. Thank you.
Richard Deeming 10-Aug-20 4:26am
   
Your StudentCourse and StudentCoursesPast entities don't need the CourseTitle property; they need a navigation property to the Courses entity.

Relationships, navigation properties, and foreign keys - EF6 | Microsoft Docs[^]

If you look again at the code in my answer, you'll see that I told Automapper to map the view-model's CourseTitle property using the Course navigation property.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900