Create mapping to the foreign key

BAD_SEED

Suppose I have this Domain Model:

public class Department
{
    public int DepartmentId { get; set; }
    public String DepartmentName { get; set; }
    public ICollection<Course> Courses { get; set; }
}

public class Course
{
    public String CourseName { get; set; }
    public String CourseDescription { get; set; }
}

and a relative Persistence Model, optimized for Entity Framework and Code First database creation:

public class DepartmentDTO
{
    public int DepartmentId { get; set; }
    public String DepartmentName { get; set; }
    public ICollection<CourseDTO> Courses { get; set; }
}

public class CourseDTO
{
    public int CourseId { get; set; }
    public int DepartmentId { get; set; }
    public String CourseName { get; set; }
    public String CourseDescription { get; set; }
}

and then I have a test class, where I make all mapping using Automapper:

    static void Main(string[] args)
    {
        Department dep = CreateDepartment();

        Mapper.CreateMap<Department, DepartmentDTO>();
        Mapper.CreateMap<Course, CourseDTO>()
              .ForMember(dst => dst.CourseId, src => src.UseValue<int>(13)); // in my real scenario this is an autogenerated GUID

        DepartmentDTO depDTO = Mapper.Map<DepartmentDTO>(dep);
    }

    public static Department CreateDepartment()
    {
        ICollection<Course> listOfCourses = new List<Course>();
        listOfCourses.Add(new Course { CourseDescription = "A dummy course", CourseName = "Test Course" });
        return new Department { DepartmentId = 42, DepartmentName = "Test Department", Courses = listOfCourses };
    }

So far so good, the map is almost complete, and only one thing is missing: the CourseDTO.DepartmentId should be 42.

In this case everything is fine cause I can access to the value through dep.DepartmentId, but this is not the real scenario.

How can I configure AutoMapper to reach my goal?

Yuliam Chandra

You can create custom Resolver to set the DepartmentId for each Course when resolving Courses.

public class CourseResolver : ValueResolver<Department, ICollection<CourseDTO>>
{
    protected override ICollection<CourseDTO> ResolveCore(Department source)
    {
        var coursesDto = source.Courses
            .Select(c => Mapper.Map<CourseDTO>(c)).ToList();
        coursesDto.ForEach(c => c.DepartmentId = source.DepartmentId);
        return coursesDto;
    }
}

Configuration:

 Mapper.CreateMap<Department, DepartmentDTO>()
     .ForMember(obj => obj.Courses, opt => opt.ResolveUsing<CourseResolver>());
 Mapper.CreateMap<Course, CourseDTO>();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Create mapping to the foreign key

From Dev

Foreign Key Mapping

From Dev

Creating a foreign key mapping in hibernate

From Dev

Creating a foreign key mapping in hibernate

From Dev

Mapping JPA Composite Foreign Key

From Dev

Mapping of primary key as foreign key to another table

From Dev

Mapping a composite foreign key to a composite primary key

From Dev

Create foreign key to a view

From Dev

MariaDB - Create Foreign key

From Dev

create phpMyAdmin foreign key

From Dev

Create foreign key to a view

From Dev

OneToOne bidirectional mapping foreign key auto fill

From Dev

JPA, Hibernate: OneToOne mapping with foreign key only

From Dev

Hibernate One to One mapping Foreign Key null

From Dev

Primary And Foreign key mapping between view and table

From Dev

Mapping (join) create table not allowing foreign keys?

From Dev

How to create foreign key in a table?

From Dev

InnoDb how to create a foreign key

From Dev

create primary and foreign key relationship

From Dev

How to create a foreign key in phpmyadmin

From Dev

Unable to create foreign key in PHPmyadmin

From Dev

Foreign key constraint error on create

From Dev

Unable to create table with foreign key

From Dev

ViewModel with foreign key and Create action

From Dev

How to create object with Foreign Key

From Dev

Entity Framework 6.1.3 Mapping Foreign key to non primary key

From Dev

create a foreign key on a primary key of another table

From Dev

Create composite key from foreign key and DateTime

From Dev

NHibernate mapping One-to-One with Foreign Key Association